react-redux v7 useSelector not re-render

Created on 18 Jun 2019  路  1Comment  路  Source: reduxjs/react-redux

This code used to work in react-redux v7.1.0
This is my demo.
https://codesandbox.io/s/trusting-sun-07mrh

const todo = useSelector(state => state.todo); not re-render
const todo = useSelector(state => state.todo , () => {}); it's re-render

Most helpful comment

You're mutating in the reducer:

    case "add":
      const { todo } = state;
      todo.push(action.payload);
      return Object.assign({}, state, { ...state, todo }); 

That is not a proper immutable update. Don't do that :)

I'd encourage you to ]use the configureStore() function from Redux Starter Kit](https://redux-starter-kit.js.org/api/configureStore), which will throw errors when you mutate.

>All comments

You're mutating in the reducer:

    case "add":
      const { todo } = state;
      todo.push(action.payload);
      return Object.assign({}, state, { ...state, todo }); 

That is not a proper immutable update. Don't do that :)

I'd encourage you to ]use the configureStore() function from Redux Starter Kit](https://redux-starter-kit.js.org/api/configureStore), which will throw errors when you mutate.

Was this page helpful?
0 / 5 - 0 ratings