使用null反应+ redux reducer处理

时间:2016-12-05 08:40:13

标签: reactjs redux immutable.js

有一些REDx存储的immutablejs Map。它需要添加"历史"项目到字段。

    "data":{
        "country": {
            "value": "US",
            "status": "locked",
            "history": null
        },
        "city": {
            "value": "NY",
            "status": "rejected",
            "history": [
                {
                    "value": "NY",
                    "date": 1447366980,
                    "reason": "bad"
                },
                {
                    "value": "WA",
                    "date": 1447331640,
                    "reason": "badbadnotgood"
                }
            ]
        },
        "event_title": {
            "value": "test",
            "status": "new",
            "history": null
        }
    }

处理reducer的这个帮助:

case SET_HISTORY:
        return state
            .updateIn(['data', payload.item, 'history'],
                item => item.push(new Map({
                    reason: payload.value,
                    date: payload.date,
                    value: payload.value
                }))
            )

但如果历史记录为空,则会出现无法推送未定义的错误。

处理它的最佳做法是什么?

0 个答案:

没有答案