Reputation: 387
I want to access a specific indexed object from object of array of REDUX STORE in REDUX REDUCER. But what would be the best and simplest way to do it.
This is the object (array of object)
const formData = [
{
name : 'Aman jat',
gender : 'male'
},
{
name : 'Ravina',
gender : 'female'
}
]
This is what i want to do ::
const reducer = (state=formData,action) => {
if(action.type=='INSERT_INFO') {
return {
...state,
state[0].name : action.payload_name1 <<<< this is my problem
}
}
else
return state
}
Upvotes: 2
Views: 253
Reputation: 44086
If I'm reading this correctly as "you want to update the name property of the first object in the state array", you probably want to do
return [
{ ...state[0], name: action.payload_name1 },
...state.slice(1)
]
Seeing that you are just learning I have the usual PSA though: This is a pretty old style of redux that we are not really encouraging to teach any more. If you are following a tutorial teaching you this, that tutorial is probably very outdated. Please follow the official redux tutorials over at https://redux.js.org/tutorials/index for up-to-date information. In modern redux with the officially recommended redux toolkit (and only there, not with the style you are using!), this could actually be written as
state[0].name = action.payload_name1
Upvotes: 2