React: problems with …spreading
React: problems with …spreading
I'm trying to recreate the Redux example from Dan Abramov's course. Spreading the ...store.getState()
at App level does not work, Redux is changing the state and React does not re-render.
...store.getState()
When I replace line 121 with line 123, the code throws an error.
Full code is here: https://github.com/asaadsaad/redux-react-code/blob/master/13-redux.html
State object: todos: [id, text, completed], visivility: 'ALL'
todos: [id, text, completed], visivility: 'ALL'
This line works fine:<TodoApp todos=store.getState().todos visibility=store.getState().visivility />
<TodoApp todos=store.getState().todos visibility=store.getState().visivility />
This line does not work: <TodoApp ...store.getState() />
<TodoApp ...store.getState() />
Why is this happening?
1 Answer
1
You have a typo in your combineReducer
's part:
combineReducer
const appReducer = combineReducers(
todos: todos_reducer,
visivility: visibility_reducer
)
Change it to: visivility -> visibility
visivility -> visibility
So, why this works with the original one? See:
visibility=store.getState().visivility
Here, you are passing store.getState().visivility
state as visibility
to your App. Typo fixed here by luck. But when you spread the object visivility
goes as it is.
store.getState().visivility
visibility
visivility
You are welcome. It happens all the time. Sometimes we need a break :) By the way, just try to follow the data like this kind of situation. At first, I did not see the typo. With a couple of
console.log
I see that actually the code can add a todo
to the store then figure out visibility
is undefined in the TodosList
component. The rest was easy.– devserkan
Aug 30 at 2:33
console.log
todo
visibility
TodosList
Appreciate it.. Thanks.
– Asaad Saad
Aug 30 at 2:36
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thanks a lot.. It happens after 10 hours of reading and coding..
– Asaad Saad
Aug 30 at 2:29