简体   繁体   English

组件在状态更新时未重新呈现

[英]Component is not re-rendering on state update

The component is not rendering on state update, even the mapStateToProps is executing successfully. 该组件不会在状态更新时呈现,即使mapStateToProps成功执行。

Component: 零件:

import React from 'react';
import AddItem from './addItemComponent';
import { connect } from 'react-redux';
import RenderItem from './RenderItem';

class HomeComponent extends React.Component {

    constructor(props) {
        super(props);

    }
    render() {

        return (
            <div>

                <div className="container">
                    <AddItem />
                </div>

                <div className="container">
                    {this.props.Items.length}
                </div>


            </div>
        );
    }
}

function mapStateToProps(state) {

    return {
        Items: state.auth.items

    }

}
export default connect(mapStateToProps)(HomeComponent);

Reducers: 减速器:

      const ItemsReducers = (state = {}, Action) => {
        if (!state.items)
            state.items = [];

        console.log("inside the reducers", state);
        var newState = Object.assign({}, state);

        switch (Action.type) {
            case 'AddItem':

                newState.items.push(Action.payload);

                return newState;
                break;

            default:
                return newState;
                break;

        }
        return newState;
    }

export default ItemsReducers;

To avoid mutation, I just clone the object and doing push operation. 为了避免发生突变,我只克隆对象并执行推入操作。 But still facing issue. 但是仍然面临问题。 It would be more helpful if I got any help. 如果我有任何帮助,那会有所帮助。

Object.assign() make a shallow copy of your object, not a deep copy. Object.assign()创建对象的浅表副本,而不是深层副本。 So, you have a mutation ! 因此,您有突变!

In your example, mapStateToProps is executing because state has changed, but items keep the same reference. 在您的示例中,由于state已更改,所以mapStateToProps正在执行,但是items保持相同的引用。 So your old items is equal to the new items , even if the length of array has changed. 因此,即使数组的长度已更改,您的旧items也将与新items相同。 And mapStateToProps do not rerender the component if the output hasn't changed. 如果输出未更改,则mapStateToProps不会重新渲染组件。

You need to copy your state and items like this: 您需要复制状态和项目,如下所示:

var newState = Object.assign({}, state);
newStats.items = newState.items.slice();

But it's not a good way to use redux. 但这不是使用redux的好方法。

I recommend to use immutability-helper package with update(). 我建议将不可变性帮助程序包与update()一起使用。 It create a copy of your object without change the original source 它创建对象的副本,而无需更改原始源

import update from 'immutability-helper';

const reducer = (state = {}, action) => {

    switch (action.type) {

        case: 'addItem': 
            return !state.items
                ? update(state, {items: {$set: [action.payload]}})
                : update(state, {items: {$push: [action.payload]}})

        default:
            return state;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM