1

My app has 2 tabs and I need to modify the content according the click in tab I have this state:

const example = {
  tabs: [
    {
      id: 111,
      title: 'Python',
      icon: '[email protected]',
      content: 'Hello World'
    },
    {
      id: 333,
      title: 'EQL',
      icon: '[email protected]',
      content: 'Hello World 2'
    }
  ]
}

and I need to change the 'content' inside of tabs. I tried this:

return {
    ...state,
    tabs: {
      ...state.tabs,
      content: 'Uhuuuul'
    }
}

But this didn't work! Could someone explain why?

1 Answer 1

1

Use map:

return {
    ...state,
    tabs: state.tabs.map(tab => ({...tab, content: "Uhuuuul"}))
}
Sign up to request clarification or add additional context in comments.

4 Comments

And how can I update only a specific tab? I tried this, but it is wrong: ``` return { ...state, tabs: state.tabs.map( tab => { if(tab.id === action.id) { ({...tab, content: action.content}) } } ) }```
return { ...state, tabs: state.tabs.map( tab => tab.id === action.id ? ({...tab, content: action.content}) : tab ) }
yours not working because you're not returning anything. if the arrow function has {} block, you need to use return. but if it uses ({}), it would return the object.
omg! i love you, guys! hahaha thanks so much, @Jack Bashford and kkesley

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.