最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - react-redux update item in array doesn't re-render - Stack Overflow

programmeradmin1浏览0评论

I have a reducer which returns an object with an array in the object. I render the list of items in the array and when the user clicks on that item I want to re-render the array (or at least the item). I've created a jsbin that shows the problem:

,console,output

To reproduce the issue, click the + or - button a few times to create a history. then click on one of the history items. you'll notice that the console log notices the event, and updates the state, but the list is not re-rendered. This can be verified by clicking the +/- button again after clicking on an item in the list. after that you'll see that it renders correctly.

Question is why does react-redux not cause this to be re-rendered? is there something I need to do to force the issue?

Thanks in advance.

I have a reducer which returns an object with an array in the object. I render the list of items in the array and when the user clicks on that item I want to re-render the array (or at least the item). I've created a jsbin that shows the problem:

https://jsbin./fadudeyaru/1/edit?js,console,output

To reproduce the issue, click the + or - button a few times to create a history. then click on one of the history items. you'll notice that the console log notices the event, and updates the state, but the list is not re-rendered. This can be verified by clicking the +/- button again after clicking on an item in the list. after that you'll see that it renders correctly.

Question is why does react-redux not cause this to be re-rendered? is there something I need to do to force the issue?

Thanks in advance.

Share Improve this question edited Sep 23, 2016 at 21:01 Ben asked Sep 23, 2016 at 18:59 BenBen 16.5k24 gold badges82 silver badges122 bronze badges 2
  • IIRC, returning an object from a reducer does only a shallow copy... perhaps the array isn't getting updated? – Kryten Commented Sep 23, 2016 at 19:02
  • no, it is getting updated... you can see this in both the console.log output as well as by selecting a few history items and then clicking the + or - button... – Ben Commented Sep 23, 2016 at 20:46
Add a ment  | 

1 Answer 1

Reset to default 6

State in redux is immutable. This means that reducer should create a new state for every mutation. Preferably, a deep clone should be done when arrays are present. The following code does an approximate deep clone for your code to work. Try utilities like lodash/deepClone for an easier solution.

const counter = (state = {count:0, history:[]}, action) => {
  let {count} = state;
  let history = [...state.history];

  switch (action.type) {
    case 'SELECT':
      history[action.i].selected = true;
      break;
    case 'INCREMENT':
      history.push({count,selected:false});
      count++;

      break;
    case 'DECREMENT':
      history.push({count,selected:false});
      count--;
      break;
    default:
      break;
  }
    console.log("count reducer: ", {count,history})
  return {count,history};
} 
发布评论

评论列表(0)

  1. 暂无评论