question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Local state change triggering Redux's props change

See original GitHub issue

What is the current behavior? Below code normally must change active component state just. But when use “return” in setState, changing props.dailyTasks of parent component.

  _checkAll() {
    const {checkAll} = this.state

    this.setState(prevState => ({
      checkAll: !checkAll,
      dailyTasks: prevState.dailyTasks.map(
        (el) => {
          el.checked = !checkAll
          return el
        }
      )
    }))

  }

What is the expected behavior? Below code works normally. Only the local state is changing.

  _checkAll() {
    const {checkAll} = this.state

    this.setState(prevState => ({
      checkAll: !checkAll,
      dailyTasks: prevState.dailyTasks.map(
        el => true ? { ...el, checked: !checkAll } : el
      )
    }))

  }

Which versions of React, ReactDOM/React Native, Redux, and React Redux are you using? Which browser and OS are affected by this issue? Did this work in previous versions of React Redux?

    "react": "16.8.3",
    "react-dom": "16.8.3",
    "react-native": "https://github.com/expo/react-native/archive/sdk-35.0.0.tar.gz",
    "react-redux": "^7.1.1",
    "redux": "^4.0.4",

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
markeriksoncommented, Nov 9, 2019

@aytaceminoglu Your description of what the “bug” is isn’t exactly clear. But yes, you’re mutating state there. See https://redux.js.org/recipes/structuring-reducers/immutable-update-patterns and https://daveceddia.com/react-redux-immutability-guide/ for details.

0reactions
aytaceminoglucommented, Nov 9, 2019

Hi @markerikson thanks for info, i will be careful. You think that’s what caused the bug?

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-redux : listening to state changes to trigger action
You want this state change C to trigger an asynchronous action X regardless of the action that originally caused the state change C....
Read more >
How useSelector can trigger an update only when we want it to
Redux has an ever-changing store, but the way we access the state of the store is through its getState method. The reference to...
Read more >
Actions and reducers: updating state - Human Redux
In Redux all changes to the application state happen via an "action. ... In Redux we say that actions (reports) are "dispatched" to...
Read more >
How To Manage State in React with Redux - DigitalOcean
You can use a mix of local state and context to manage state. ... data into your components and dispatch new changes to...
Read more >
Component state: local state, Redux store, and loaders
In a component built with ES6 classes, whenever the state changes (only available through setState function), React triggers a re-render ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found