1

I have this function that returns an array of objects, every object represent a sticky, what I want is to change the value of "content" everytime I click one of the stickies

  handleStickyEdition = (index) => {
    const { currentStage } = this.props
    const stickies = currentStage.get('stickies')
    const updatedStickies = [...stickies]
    console.log(updatedStickies)
  }

And the result of calling the console.log is this array of objects: enter image description here

If I do console.log(updatedStickies[index].get('content')) I will get the content of the object I want to change. For example name 3.

How can I replace this content with an empty string? in other words, if I click the object in the position 0, how can I make name 3 equals to ''

2
  • updatedStickies[index].content = ' ' Commented Mar 6, 2018 at 2:27
  • 1
    @guijob This is incorrect, you are trying to mutate the data inside of props. Commented Mar 6, 2018 at 19:22

1 Answer 1

1

I would suggest using a map like so.

this.setState({
  updatedStickies: this.state.updatedStickes.map(sticky => ({
    ...sticky
    content: sticky.id === idOfStickyWeWantToUpdate ? "" : "content"
  }))
});

I see you are reading stickies from props, I would suggest having a function in your parent component to run the above code which you can call from your child component if need be.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.