【问题标题】:Change in state not being rendered with object.map未使用 object.map 呈现状态变化
【发布时间】:2019-08-13 15:30:09
【问题描述】:

redux 存储的状态正在改变,但无法让 object.map 函数重新渲染新状态。收到以下错误:“TypeError: Cannot read property 'map' of undefined”

确认actions.js中的数据正确,确认reducer.js中的数据正确,确认state.PrepInfos中的状态变化正确。

表格:

class PrepInfos extends Component {
    render(){
        const{ PrepInfos } = this.props;

        return(
                <Form>
                    {PrepInfos.map(prepInfo => <PrepInfo key={prepInfo.id} id={prepInfo.id} type={prepInfo.type} quantity={prepInfo.quantity} description={prepInfo.description} />)}
                </Form>
        );
    }
}


const mapStateToProps = state => ({
    PrepInfos: state.recipeForm.PrepInfos.PrepInfos,
});

const mapDispatchToProps = (dispatch) => bindActionCreators({

}, dispatch);

export default connect(mapStateToProps, mapDispatchToProps)(PrepInfos);

动作:

export const H_CHANGE = 'H_CHANGE';

export function hChange(event) {

    const form = ({
        value: event.target.value,
        name: event.target.name,
        id: event.target.id,
    });

    return ({
        type: 'H_CHANGE',
        data: form,
    });
}

减速机:

import { H_CHANGE } from './PrepInfo/actions';

const initialState = {
    PrepInfos: [{id:0, type:"makes", quantity:30, description:"slices"}, {id:1, type:"chill", quantity:15, description:"minutes"}],
};

export default function(state = initialState, action){

    const { type, data } = action;

    switch(type) {

        case H_CHANGE:

            return state.PrepInfos.map(prepInfo => {
                if (prepInfo.id == data.id) {
                    return {...prepInfo, [data.name]: data.value}
                };
                return prepInfo;
            });

        default:
            return state;
    }
}

更正减速器:

return Object.assign({}, state, {
    PrepInfos: state.PrepInfos.map(prepInfo => {
        if (prepInfo.id == data.id) {
            return {...prepInfo, [data.name]: data.value}
            };
        return Object.assign({}, prepInfo, {});
    })
})

期望重新渲染新状态,而不是得到 TypeError: Cannot read property 'map' of undefined

【问题讨论】:

  • PrepInfos 是如何进入props 的?您要么缺少connect 函数,要么需要显示更多代码
  • @EricHasselbring 刚刚添加了连接代码

标签: reactjs redux rendering reduce


【解决方案1】:

这个bug是reducer中的mutating state引起的

        // this is mutating the PrepInfos property in state
        return state.PrepInfos.map(prepInfo => {
            if (prepInfo.id == data.id) {
                return {...prepInfo, [data.name]: data.value}
            };
            return prepInfo;
        });

        // this is creating and returning a new obj for state and the PrepInfos key in state
        return {
          ...state,
          PrepInfos: state.PrepInfos.map(prepInfo => {
            if (prepInfo.id == data.id) {
                return {...prepInfo, [data.name]: data.value}
            };
            return prepInfo;
        }

【讨论】:

  • 谢谢!我稍微重构了代码以使其正常工作,但这绝对是问题所在。我根据您的解决方案更新了问题以包含工作代码
猜你喜欢
  • 1970-01-01
  • 2020-06-20
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-16
  • 2017-08-21
相关资源
最近更新 更多