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.

Question: Always need to clone a state property in reducer?

See original GitHub issue

Hello again,

given follwing example:

// state has property 'list' which is an array().

const reducer = (state, action) => {
  switch (action.type) {
    case 'ADD_LIST_ITEM':
      state.list.push('new item');
      return Object.assign({}, state, { list: state.list });
  }
};

Using this, a polymer element property bound by ā€œstatePath: ā€˜listā€™ā€ doesn’t recognize the change of state.list.

When doing the following, everything works like desired:

const reducer = (state, action) => {
  switch (action.type) {
    case 'ADD_LIST_ITEM':
      var tmp = state.list.splice(0);
      tmp .push('new item');
      return Object.assign({}, state, { list: tmp });
  }
};

So does this mean, I’ve to copy/clone every property of the state in the reducer before changing it?

Regards, Michael

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
tur-nrcommented, Apr 11, 2017

No worries.

If you have any questions, get me on slack rather than raise issues. Unless it’s an issue of course šŸ˜‰

https://polymer.slack.com

1reaction
tur-nrcommented, Apr 11, 2017

You don’t have to use this ā€œpatternā€. Google wrote their own unidirectional, UniFlow with achieves the same goal just with a different state manager.

Whatever you use, unidirectional is the way forward. Always push your values down and use Mediators correctly. By correctly, I mean using readOnly on the properties that push state upwards.

Read more comments on GitHub >

github_iconTop Results From Across the Web

In Redux, is it necessary to do deep copy - Stack Overflow
Common Redux misconception: you need to deeply clone the state. ... Changed properties will be anyway new values, so no question of type...
Read more >
Redux Fundamentals, Part 3: State, Actions, and Reducers
The official Redux Fundamentals tutorial: learn how reducers update state in response to actions.
Read more >
Actions and reducers: updating state - Human Redux
So the resulting function takes the whole starting application state, the action to be processed, and returns the new application state.
Read more >
React Hooks cheat sheet: Best practices with examples
To help demonstrate how to solve common React Hooks questions, ... The useReducer call returns the state property and a dispatch function.
Read more >
Returning the State from the Reducer - Understanding Redux
You should not mutate the state received in your Reducer. Instead, you should always return a new copy of the state. Technically, you...
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