redux/react 应用程序中的状态有一个 属性 和减速器的名称
State in redux/react app has a property with the name of the reducer
我正在使用 Redux 和 React 创建一个应用程序。我 运行 遇到一个问题,我无法将状态映射到组件属性,因为状态有一个 属性 与我使用的减速器的名称相匹配。
root reducer 是用 combineReducers
方法创建的
const rootReducer = combineReducers({
appReducer
});
初始状态为
const initialState = {
sources: [],
left: {},
right: {},
diff: {}
}
但是在组件函数中mapStateToProps
:
function mapStateToProps(state) {
return {
sources: state.sources
}
}
state.sources
是undefined
因为state
参数的值是
{
appReducer: {
sources: [],
left: {},
right: {},
diff: {}
}
}
这是redux的特性吗?所以当我使用更多的 reducer 时,它们都会添加新的 属性 到 state
变量?还是我这边有问题(我从来没有在 redux 教程中注意到这种行为)。
谢谢
实际上,我相信你的初始状态是:
{
appReducer: {
sources: [],
left: {},
right: {},
diff: {}
}
}
这是因为 combineReducers
通过获取 reducer 的名称并将其内容映射到该名称来工作。
此外,请注意,如果您要使用多个 reducer,则您的 reducer 的名称应该比 appReducer
更具体,并且(只是我个人的意见)他们不会不需要 reducer
这个词。典型的应用程序可能如下所示:
combineReducers({
user: userReducer,
messages: messagesReducer,
notifications: notificationsReducer
});
然后,您的状态可以像这样访问:
state.user.email
state.messages[0]
如果你只有一个减速器,你不需要combineReducers()
。直接使用就可以了:
const initialState = {
sources: [],
left: {},
right: {}
}
function app(state = initialState, action) {
switch (action.type) {
case 'ADD_SOURCE':
return Object.assign({}, state, {
sources: [...state.sources, action.newSource]
})
case 'ADD_SOURCE_TO_LEFT':
return Object.assign({}, state, {
left: Object.assign({}, state.left, {
[action.sourceId]: true
})
})
case 'ADD_SOURCE_TO_RIGHT':
return Object.assign({}, state, {
right: Object.assign({}, state.right, {
[action.sourceId]: true
})
})
default:
return state
}
}
现在您可以使用该减速器创建商店:
import { createStore } from 'redux'
const store = createStore(app)
并将组件连接到它:
const mapStateToProps = (state) => ({
sources: state.sources
})
但是你的 reducer 很难阅读,因为它会同时更新很多不同的东西。现在,这个就是你想把它拆分成几个独立的reducer的时候了:
function sources(state = [], action) {
switch (action.type) {
case 'ADD_SOURCE':
return [...state.sources, action.newSource]
default:
return state
}
}
function left(state = {}, action) {
switch (action.type) {
case 'ADD_SOURCE_TO_LEFT':
return Object.assign({}, state, {
[action.sourceId]: true
})
default:
return state
}
}
function right(state = {}, action) {
switch (action.type) {
case 'ADD_SOURCE_TO_RIGHT':
return Object.assign({}, state, {
[action.sourceId]: true
})
default:
return state
}
}
function app(state = {}, action) {
return {
sources: sources(state.sources, action),
left: left(state.left, action),
right: right(state.right, action),
}
}
这更容易维护和理解,也更容易独立更改和测试减速器。
最后,作为最后一步,我们可以使用combineReducers()
生成根app
reducer,而不是手动编写:
// function app(state = {}, action) {
// return {
// sources: sources(state.sources, action),
// left: left(state.left, action),
// right: right(state.right, action),
// }
// }
import { combineReducers } from 'redux'
const app = combineReducers({
sources,
left,
right
})
使用 combineReducers()
而不是手动编写 root reducer 并没有太大的好处,除了它稍微更有效率并且可能会避免一些错别字。此外,您可以在您的应用程序中多次应用此模式:以嵌套方式多次将不相关的 reducer 组合成一个 reducer 是可以的。
所有这些重构都不会影响组件。
我建议您观看我的 free Egghead course on Redux,其中介绍了 reducer 组合 的这种模式,并展示了 combineReducers()
是如何实现的。
我正在使用 Redux 和 React 创建一个应用程序。我 运行 遇到一个问题,我无法将状态映射到组件属性,因为状态有一个 属性 与我使用的减速器的名称相匹配。
root reducer 是用 combineReducers
方法创建的
const rootReducer = combineReducers({
appReducer
});
初始状态为
const initialState = {
sources: [],
left: {},
right: {},
diff: {}
}
但是在组件函数中mapStateToProps
:
function mapStateToProps(state) {
return {
sources: state.sources
}
}
state.sources
是undefined
因为state
参数的值是
{
appReducer: {
sources: [],
left: {},
right: {},
diff: {}
}
}
这是redux的特性吗?所以当我使用更多的 reducer 时,它们都会添加新的 属性 到 state
变量?还是我这边有问题(我从来没有在 redux 教程中注意到这种行为)。
谢谢
实际上,我相信你的初始状态是:
{
appReducer: {
sources: [],
left: {},
right: {},
diff: {}
}
}
这是因为 combineReducers
通过获取 reducer 的名称并将其内容映射到该名称来工作。
此外,请注意,如果您要使用多个 reducer,则您的 reducer 的名称应该比 appReducer
更具体,并且(只是我个人的意见)他们不会不需要 reducer
这个词。典型的应用程序可能如下所示:
combineReducers({
user: userReducer,
messages: messagesReducer,
notifications: notificationsReducer
});
然后,您的状态可以像这样访问:
state.user.email
state.messages[0]
如果你只有一个减速器,你不需要combineReducers()
。直接使用就可以了:
const initialState = {
sources: [],
left: {},
right: {}
}
function app(state = initialState, action) {
switch (action.type) {
case 'ADD_SOURCE':
return Object.assign({}, state, {
sources: [...state.sources, action.newSource]
})
case 'ADD_SOURCE_TO_LEFT':
return Object.assign({}, state, {
left: Object.assign({}, state.left, {
[action.sourceId]: true
})
})
case 'ADD_SOURCE_TO_RIGHT':
return Object.assign({}, state, {
right: Object.assign({}, state.right, {
[action.sourceId]: true
})
})
default:
return state
}
}
现在您可以使用该减速器创建商店:
import { createStore } from 'redux'
const store = createStore(app)
并将组件连接到它:
const mapStateToProps = (state) => ({
sources: state.sources
})
但是你的 reducer 很难阅读,因为它会同时更新很多不同的东西。现在,这个就是你想把它拆分成几个独立的reducer的时候了:
function sources(state = [], action) {
switch (action.type) {
case 'ADD_SOURCE':
return [...state.sources, action.newSource]
default:
return state
}
}
function left(state = {}, action) {
switch (action.type) {
case 'ADD_SOURCE_TO_LEFT':
return Object.assign({}, state, {
[action.sourceId]: true
})
default:
return state
}
}
function right(state = {}, action) {
switch (action.type) {
case 'ADD_SOURCE_TO_RIGHT':
return Object.assign({}, state, {
[action.sourceId]: true
})
default:
return state
}
}
function app(state = {}, action) {
return {
sources: sources(state.sources, action),
left: left(state.left, action),
right: right(state.right, action),
}
}
这更容易维护和理解,也更容易独立更改和测试减速器。
最后,作为最后一步,我们可以使用combineReducers()
生成根app
reducer,而不是手动编写:
// function app(state = {}, action) {
// return {
// sources: sources(state.sources, action),
// left: left(state.left, action),
// right: right(state.right, action),
// }
// }
import { combineReducers } from 'redux'
const app = combineReducers({
sources,
left,
right
})
使用 combineReducers()
而不是手动编写 root reducer 并没有太大的好处,除了它稍微更有效率并且可能会避免一些错别字。此外,您可以在您的应用程序中多次应用此模式:以嵌套方式多次将不相关的 reducer 组合成一个 reducer 是可以的。
所有这些重构都不会影响组件。
我建议您观看我的 free Egghead course on Redux,其中介绍了 reducer 组合 的这种模式,并展示了 combineReducers()
是如何实现的。