I have an issue where re-rendering of state causes ui issues and was suggested to only update specific value inside my reducer to reduce amount of re-rendering on a page.
this is example of my state
JavaScript
x
9
1
{
2
name: "some name",
3
subtitle: "some subtitle",
4
contents: [
5
{title: "some title", text: "some text"},
6
{title: "some other title", text: "some other text"}
7
]
8
}
9
and I am currently updating it like this
JavaScript
1
3
1
case 'SOME_ACTION':
2
return { state, contents: action.payload }
3
where action.payload
is a whole array containing new values. But now I actually just need to update text of second item in contents array, and something like this doesn’t work
JavaScript
1
3
1
case 'SOME_ACTION':
2
return { state, contents[1].text: action.payload }
3
where action.payload
is now a text I need for update.
Advertisement
Answer
You could use the React Immutability helpers
JavaScript
1
13
13
1
import update from 'react-addons-update';
2
3
// ...
4
5
case 'SOME_ACTION':
6
return update(state, {
7
contents: {
8
1: {
9
text: {$set: action.payload}
10
}
11
}
12
});
13
Although I would imagine you’d probably be doing something more like this?
JavaScript
1
9
1
case 'SOME_ACTION':
2
return update(state, {
3
contents: {
4
[action.id]: {
5
text: {$set: action.payload}
6
}
7
}
8
});
9