Why change Redux store?

Hello.
See here is a very strange behavior in a functional component
Stack React / Redux

This situation.

Functional component via props get the array from the redux store

For example
const GroupUsersList = (props) => {
 const modules = props.data 

 if (something there) {
 modules.users.push({ _id: user._id, name: `${user.Name} (${user.login})` })
}
 return (
<>
</>
)
}
export default GroupUsersList;


But after I change the array using one of the methods of the array (push, splice
etc.) I then change the data in the store redux

What can be the reason?
April 7th 20 at 15:29
1 answer
April 7th 20 at 15:31
Solution
Because the objects (and arrays) in js are passed by reference. You positi in the array lying in bed. It is obvious that the array is changing.
firstly to change the data in the first store they still need to thedispatcher no matter what type they are, but nothing like I do not

secondly, by your logic
const modules = { ...props.data }

should help, but alas....here is something else - Kara.Cremin commented on April 7th 20 at 15:34
@Theresia, the Concept of "Mutation" you about something said? You said it right, you posite data in the object by reference. When dispatched, the log is visible action and redex store is updated with the new value NOT by reference, react understands that the props came new object and redraws it. In your case, when the object is updated the link reakt does not understand and is not redrawn in stor already changed. - cleo.Turner commented on April 7th 20 at 15:37
firstly to change the data in the first store they still need to thedispatcher

not "needed". A "correct - change via dispatch". And you change the wrong way - directly changing the objects in the store.

should help

not be. From what you scanrate object props, array inside not skaniruesh - will remain the same as it was.

x=[1,2,3]
c={a: x}
d={c...}
d.a[0] = 10

console.log(x) // see for yourself that there will be
- luz_Stroman commented on April 7th 20 at 15:40
thank you very much! understood! I didn't realize that nested inside objects and arrays are also passed by reference! - Kara.Cremin commented on April 7th 20 at 15:43
@claire.Be, ie correct any changes (add, delete, edit) in the arrays to produce reducer?

I have not so much experience, so I change the array in the component (often still in the container) and the result is the dispatch. - Kara.Cremin commented on April 7th 20 at 15:46
@Theresia, can be different, but the rule is:
If you get something in the props in the component - it can not be changed in any way.
Either change logic or do a full copy and it has changed.
you can clone in x.slice() for example.

(or take mobx and change what you want and how you want not bothered ;)) - luz_Stroman commented on April 7th 20 at 15:49

Find more questions by tags ReactRedux