- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 header 组件,应该在获取登录数据后更新。它检查用户名并显示欢迎消息。
在不使用combineReducers
创建商店时,仅使用loginReducer
我创建的一切工作正常,状态得到更新,然后组件也得到更新。然而,当使用combineReducers时,即使我在其中使用相同的单个reducer,组件也会停止更新。我还使用记录器中间件来显示状态更改,并且状态似乎已正确更新。
请注意,我在这里仅显示相关文件部分。
index.js
const loggerMiddleware = createLogger();
// Here I'm creating the store with a single reducer function
const store = createStore(loginReducer, applyMiddleware(thunkMiddleware, loggerMiddleware));
const router = createRouter();
ReactDOM.render(<Provider store={store}>
{ router }
</Provider>,
document.getElementById('root')
);
loginReducer.js
// Please notice that here I'm not mutating state in any way
// I'm always returning a new state as recommended by the docs
const ACTION_HANDLERS = {
[REQUEST_LOGIN]: (state, action) => {
return ({ ...state, username: action.payload.username, authHeader: 'Basic ' + base64Encode(action.payload.username + ':' + md5(action.payload.password))});
},
[RECEIVE_LOGIN]: (state, action) => {
return ({ ...state, resources: action.payload.resources, isCashier: action.payload.isCashier });
},
[LOGOUT]: (state) => {
return ({ ...state, username: null, resources: {}, authHeader: null, isCashier: null });
}
}
const initialState = { isCashier: null, resources: {}, username: null };
export default function loginReducer (state = initialState, action) {
const handler = ACTION_HANDLERS[action.type]
return handler ? handler(state, action) : state
}
export default loginReducer;
LoginContainer.js
const mapActionCreators = {
doLogin,
doLogout
}
const mapStateToProps = (state) => ({
resources: state.resources,
username: state.username
})
export default connect(mapStateToProps, mapActionCreators)(Login)
<小时/>
这是不起作用的版本,我所做的唯一更改是:* 我现在将 loginReducer
包装在 combineReducers
中* 我更改了 mapStateToProps 函数,以便从状态内的嵌套 login
对象获取这些属性
index.js
const rootReducer = combineReducers({
login: loginReducer
});
const loggerMiddleware = createLogger();
// Now I'm not using the raw `loginReducer` anymore
const store = createStore(rootReducer, applyMiddleware(thunkMiddleware, loggerMiddleware));
const router = createRouter();
ReactDOM.render(<Provider store={store}>
{ router }
</Provider>,
document.getElementById('root')
);
loginReducer.js
-> 这与上面相同
LoginContainer.js
const mapActionCreators = {
doLogin,
doLogout
}
// Here I'm getting properties from `state.login` instead of `state` now due to the use of combineReducers
const mapStateToProps = (state) => ({
resources: state.login.resources,
username: state.login.username
})
export default connect(mapStateToProps, mapActionCreators)(Login)
这是一张图片,我在其中描述了分派(dispatch)每个操作后的状态:
每当调度一个操作并记录上一个和下一个状态时,它们都是正确的,但我的组件不会更新。
我在这里遗漏了一些东西还是这确实是一个错误?
最佳答案
请测试您的 reducer 。 请保持状态平稳。 使用高阶组件进行身份验证逻辑。使用重新选择来提高性能。这是您减少的错误测试。这应该有效。深度嵌套状态不好,因为它会让你的状态难以思考和管理。我的代码如下:
import { combineReducers } from 'redux';
//import the reducers ou want
const authReducer = combineReducers({
loginData: LoginReducer,
permission: permissionReducer
});
export default rootReducer;
关于javascript - 使用combineReducers后组件停止更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37822647/
请告诉我我做错了什么?配置商店.js: import {configureStore, combineReducers} from "@reduxjs/toolkit"; import pokemon
请告诉我我做错了什么?配置商店.js: import {configureStore, combineReducers} from "@reduxjs/toolkit"; import pokemon
我正在使用 combineReducers在我的 React TypeScript 应用中: // combinedReducer.ts import { combineReducers } from
我有bookManageReducer.jsx: import { combineReducers } from 'redux' import { REQUEST_BOOKS_PAGE, RE
我是 Redux 的新手。 我一直在阅读它的'Reducers documentation , 遇到了我似乎无法理解的他们的 combineReducers 函数。 他们注意到: All combin
我试图嵌套我的 reducer 以提供这种结构: state: { data: { dataSetOne: { items: [], filter: {
我有一个 redux 存储,到目前为止,它非常简单,只需要一个 reducer 文件。但是我现在需要把它分开。使用 combineReducers{reducerOne, reducerTwo} 是我
在 egghead.io series on Redux 的第 16 课中,在看 Dan 是如何做到的之前,我尝试实现了自己的 combineReducers 函数。我得到了以下。我尝试在像这样传入的
尝试将我的单个 reducer 拆分为不同的 reducer combineReducers是 redux 为此目的提供的便利功能。 combineReducers尽管使用至少每个第一级状态键的缩减器
我现在在 Redux 的 combineReducers 上收到以下错误。 A 缺少类型注释。 A 是在函数类型 [1] 中声明的类型参数,并在调用 combineReducers [2] 时隐式实例
当我直接使用 redux reducer 时,我得到了预期的状态: import cheese from 'reducers/cheese.js'; const store = createStore
我想要的是根 reducer 结合其他 reducer ,并听取额外的 Action 。我找到了文档,但我无法获得任何信息。 这是一些伪代码。 const root1 = combineReducer
当我使用定义的名称导入 reducers 时,我的应用程序工作正常,但是当我使用 combineReducers 时,它返回空白页。 store.js代码是: import { createStore
我之前的 React-Redux 实现工作正常,但在我尝试使用单独的文件实现 combineReducer 函数后,抛出了一个我不太明白的错误。希望你们中的一些人能帮助我! 错误:未捕获的类型错误:t
在下面的代码中,数据 (state, action) 没有被 combineReducers 传递给 reducers。它会产生以下错误。可能只是我犯的一个小错误。链接到 JSFiddle:https
更新 感谢@Dominic Tobias 和@gabdallah 发现了我令人尴尬的错误。 当然是正确答案; so try checking action.payload. 关于 switch 语句和
在Redux basics tutorial关于 Reducers 的部分我不太明白以下语法如何推断出应用程序状态的哪个子集要传递给调用 combineReducers 时引用的每个 reducer。
Redux 文档建议使用 normalizr像这样设计状态的形状: { entities: { cards: { 42: { id: 42, t
这是我的商店代码 import { combineReducers, createStore } from "redux"; import counterReducer from "./../Cou
将两个 reducers 组合在一起(EditButton 和 TodoApp)后,我的应用程序每次开始崩溃。在此之前,当我只使用一个 reducer TodoApp 时,我对 reducers 没有
我是一名优秀的程序员,十分优秀!