redux的state对象中的这些其他属性是什么?

时间:2019-01-28 19:12:37

标签: reactjs redux react-redux

我目前正在学习React Redux的原理。我正在从化简器记录状态对象。但是我看到了节点1:a,2:b等。这些是做什么用的?{enter image description here

这是我的化简代码,我只是调用了recipepuppy API

import { MAKE_REQUEST, LOAD_SEARCH_RESULTS, SEARCH_FAILED } from "../../constants/actionTypes";
const initialState = {
    isRequesting: true,
    searchTerm: '',
    searchFailed: false,
    searchSuccessful: false,
    searchResults: []
};
const rootReducer = (state = initialState, action) => {
    console.log("Original state" + state);
    switch (action.type) {
        case MAKE_REQUEST:

            let requestState = Object.assign({}, state, action.searchTerm, { isRequesting: true });
            console.log(requestState);
            return requestState;

        case LOAD_SEARCH_RESULTS:
            var results = action.searchResults.map(({ thumbnail, title, href }) => { return { thumbnail, title, href } });
            let searchResultState = Object.assign({}, state, { isRequesting: false, searchSuccessful: true, searchFailed: false, searchResults: results });
            console.log(searchResultState);
            return searchResultState;

        case SEARCH_FAILED:
            let searchFailState = Object.assign({}, state, { isRequesting: false, searchSuccessful: false, searchFailed: true, searchResults: [] });
            console.log(searchFailState);
            return searchFailState;

        default:
            return state;
    }
}

export default rootReducer;

1 个答案:

答案 0 :(得分:1)

在您的第一种情况下,我假设action.searchTerm是一个字符串。尝试在控制台中执行Object.assign({}, 'cake')。它将返回{0: "c", 1: "a", 2: "k", 3: "e"},因为您是将字符串分配给作为对象的新对象。

如果您希望自己的州拥有名为searchTerm且值为action.searchTerm的属性,请执行以下操作:

Object.assign({}, state, { isRequesting: true, searchTerm: action.searchTerm });