首页 > redux中 state已更新,但没有重新渲染。

redux中 state已更新,但没有重新渲染。

列一下核心代码:
通过点击,将state中notes中的某一项的editing属性变更为true(默认是false)
有两种写法,第一种写法会导致state更新了,但没有重新渲染。等下一次某个事件导致state更新时,会将变化一起渲染出来。

const app = (state = {}, action) => {
    switch (action.type) {
        case 'ONEDIT':
             state.notes.find((note) => note.id === action.id).editing = true;
             return {
                  notes: state.notes
             };
    }
};

另一种map写法则正常,state更新后,会正常刷新

const app = (state = {}, action) => {
    switch (action.type) {        
        case 'ONEDIT':
             return {
                notes:state.notes.map((note) => {
                    if (note.id === action.id) {
                        note.editing = true;
                    }
                    return note
                })
             };
    }
};

求大神解释为什么第一种写法不会重新渲染。感激不尽!!!


第一种方法返回的不是一个新的对象,所以不行。可以使用object.assign方法。

【热门文章】
【热门文章】