状态未初始化为reducer的初始状态

时间:2018-12-11 11:03:14

标签: javascript reactjs redux react-redux

我是React Redux的新手。在这里,我正在做的是

const initialState = {
        Low: [
            {
                id: 0,
                technologyId: 0,
                technology: '',
                level: 'EASY'
            }
        ],
        Medium: [
            {
                id: 0,
                technologyId: 0,
                technology: '',
                level: 'MEDIUM'
            }
        ],
        High: [
            {
                id: 0,
                technologyId: 0,
                technology: '',
                level: 'TOUGH'
            }
        ]
    }

现在,在我的减速器中,

 export default function QuizData(state = initialState, action) {
        switch (action.type) {
            case QUIZ_DATA:
                return {
                    ...state,
                    Low: action.data,
                    error: false,
                } 
            case RESET_QUIZ_CRITERIA: {
            console.log("intial state is ",  ...state);
            return {
                ...state
            }

现在,这里发生的是经过一些操作,此对象随着每个键具有一些值而发生更改。喜欢,

所以,这变了。

{
        Low: [
            {
                id: 0,
                technologyId: 11,
                technology: 'xsxs',
                level: 'EASY'
            }
        ],
        Medium: [
            {
                id: 0,
                technologyId: 22,
                technology: 'swwsw',
                level: 'MEDIUM'
            }
        ],
        High: [
            {
                id: 0,
                technologyId: 110,
                technology: 'xsxsx',
                level: 'TOUGH'
            }
        ]
    }

重置我的操作就像,

export function resetPreviousQuizSelectionCriteria() {
  console.log("Calling this");
  return {
    type: RESET_QUIZ_CRITERIA
  }
}

现在,我要做的是,

那时候用户单击按钮时,我想将其更改为初始状态。

因此它将没有任何值,因为它应该与默认值相同。

那么,有人可以建议我解决方案吗?

1 个答案:

答案 0 :(得分:0)

我认为返回初始状态应该可以解决问题。

 case RESET_QUIZ_CRITERIA: {
        console.log("intial state is ",  ...state);
        return {
            ...initialState
        }

尝试此操作。

相关问题