0

I have stumbled upon a problem when adding an element inside an object inside an array. What I would like to do is add the element isFavorite: true. I am currently attempting to do so by doing the folowing:

{ 
    ...state, 
    list: state.list.map(item => 
        item.id === action.payload.libraryContentId ? 
        { ...item, isFavorite: true } 
        : item) 
}

The payload contains the following object:

{
    id: 1,
    name: 'Name',
    libraryContentId: 3
}

But the above does not seem to be updating the item and add the element. What do you guys suggest?

Edit: More examples of the code.

Action:

try {
    const response = await axios.post(URL, {params});
    dispatch({ type: ADD_FAVORITE, payload: response.data });
catch (error) {
    dispatch({ type: ERROR });
}

Response Data:

{
    id: 117
    libraryConntentId: 245
}

List item sample:

{
    id: 245,
    name: 'Name',
    isFavorite: false
}

Thank you!

7
  • Does it make a difference if you add () around your overwriting statement? Like so ({ ...item, isFavorite: true }) Commented Nov 8, 2018 at 21:17
  • If the state isn't updated I also suggest you provide more code since the problem might be elsewhere than your new state object. Commented Nov 8, 2018 at 21:18
  • What you have written looks reasonable. Could you include a Minimal, Complete, and Verifiable example? There might be something else going on, like e.g. the id is a string in your state, etc. Commented Nov 8, 2018 at 21:19
  • @E.Sundin I tried your suggestion and it still won't update the state. I am trying to see where the problem may lay since the condition is being met through the iteration meaning the action is properly dispatching the payload. Commented Nov 8, 2018 at 22:07
  • @Tholle Hi Tholle, I have gone ahead and added a few examples of the objects and the action making the dispatch. The expression item.id === action.payload.libraryContentId does return the correct item when I log it to the console. Is there anything else that you see? Commented Nov 8, 2018 at 22:15

1 Answer 1

1

Maybe something like this (check testMethod):

class Hello extends React.Component {
	constructor(props){
  	super(props);
    
    
    let testData = [
    	{
      	id: 1,
        name: 'Name1',
        isFavorite: false
      },
      {
      	id: 2,
        name: 'Name2',
        isFavorite: false
      },
      {
      	id: 3,
        name: 'Name3',
        isFavorite: false
      },
      {
      	id: 4,
        name: 'Name4',
        isFavorite: false
      },
      {
      	id: 5,
        name: 'Name5',
        isFavorite: false
      }]
    
    this.state = {
    	boolvar: true,
        numbar: 2,
    	list: testData
    }
   	this.testMethod = this.testMethod.bind(this);
    console.log("Original state");
    console.log(this.state);
  }
  
  testMethod(){
  		 let testAction = {
		payload: {
    	libraryContentId: 3
     }
	}
  
   this.setState((prevState, prevProps) => {
      return {
       list: [...prevState.list.map(item => item.id === testAction.payload.libraryContentId? {...item, isFavorite: true}: item)]
       }});
   }
  render(){
  	console.log(this.state);
    return (<div><button onClick={() => {this.testMethod()}}>Test It</button></div>);
  }
  
   
}

ReactDOM.render(
  <Hello name="World" />,
  document.getElementById('container')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="container">
    <!-- This element's contents will be replaced with your component. -->
</div>

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.