[英]React Redux not updating state
I am using the React Hooks implementation of React-Redux.我正在使用 React-Redux 的 React Hooks 实现。 Below is the flow of my code.下面是我的代码流程。 For some reason any values that I pass in my dispatch(fuction(value))
is not detected in my reducer.出于某种原因,我在我的dispatch(fuction(value))
中传递的任何值都没有在我的 reducer 中检测到。 I can't figure it out.我想不通。
src/components/categories/selectCategory.tsx src/components/categories/selectCategory.tsx
import React from 'react';
import {useDispatch} from 'react-redux';
import {setCategory} from '../../store/actions';
const selectCategory = (name: string) => {
dispatch(setCategory(name)); // ex: name = 'algebra'
};
store/actions/index.ts存储/操作/index.ts
export const setCategory = (name) => ({
type: 'SET_CATEGORY',
name: name
});
store/reducers/index.ts存储/reducers/index.ts
import {combineReducers} from 'redux';
import category from './category';
const app = combineReducers({
category
});
export default app;
store/reducers/category.ts存储/reducers/category.ts
const initialState = {
name: 'calculus'
};
const category = (state = initialState, action) => {
switch (action.type) {
case 'SET_CATEGORY':
return {name: state.name}; // outputs 'calculus'
default:
return state;
}
};
export default category;
I'm sure there is some small detail I am missing.我敢肯定,我缺少一些小细节。
My issue was fixed by returning the action.name
property instead of state.name
.我的问题是通过返回action.name
属性而不是state.name
来解决的。
store/reducers/category.ts存储/reducers/category.ts
const initialState = {
name: 'calculus'
};
const category = (state = initialState, action) => {
switch (action.type) {
case 'SET_CATEGORY':
return {name: action.name};
default:
return state;
}
};
export default category;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.