gpt4 book ai didi

javascript - 这个 JavaScript 对象解构是如何工作的?

转载 作者:行者123 更新时间:2023-12-01 00:07:27 24 4
gpt4 key购买 nike

我试图理解这个 React redux 示例:https://codesandbox.io/s/f2ded

reducer.js中,初始状态是:

const initialState = {
taskName: "",
tasks: []
};

App.js中,我们有:

const mapStateToProps = state => {
return {
...state.list
};
};

state 没有属性 list,并且 state.list 应该是一个空对象{}。但是,它实际上成立

感谢任何有助于理解其具体工作原理的帮助。谢谢。

最佳答案

这是因为底部的combineReducers

const reds = combineReducers({ list });

export default reds;

这意味着 Redux 状态的该部分中的所有内容都是 state.list.taskNamestate.list.tasks 等。

[编辑]只是想从官方文档中添加一些清晰度,https://redux.js.org/api/combinereducers/

rootReducer = combineReducers({potato: potatoReducer, tomato: tomatoReducer})
// This would produce the following state object
{
potato: {
// ... potatoes, and other state managed by the potatoReducer ...
},
tomato: {
// ... tomatoes, and other state managed by the tomatoReducer, maybe some nice sauce? ...
}
}

关于javascript - 这个 JavaScript 对象解构是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60281217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com