[英]Redux dispatch won't change my initialState
Seems like dispatch is not working as it is supposed to be. 似乎调度无法正常运行。
Here is my code. 这是我的代码。
Wrapper.js Wrapper.js
...
const Wrapper = ({ action, variable }) => {
useEffect(() => {
action();
}, []);
return(
<Text>{variable.toString()}</Text>
);
};
...
Actions.js Actions.js
export const action = () => dispatch => {
dispatch({
type: MY_TYPE,
payload: true,
});
};
Reducer.js Reducer.js
...
const initialState = {
variable: false,
};
export default function(state = initialState, action) {
switch(actions.types) {
case MY_TYPE:
return {
...state,
variable: action.payload,
};
default:
return state;
}
}
...
When i do console.warn('test')
inside my action function, i get the yellow message "test" so i assume that everything is fine with my code. 当我在我的动作函数中执行
console.warn('test')
,我得到黄色消息“ test”,因此我认为我的代码一切都很好。 But given the code above, in my <Text>
i always get false
但是鉴于上面的代码,在我的
<Text>
我总是会得到false
You have typo in your reducer. 您的减速器中有错字。 There is
actions.types
. 有
actions.types
。 Should be action.type
. 应该是
action.type
。
Also, I see you use function instead of object for an action. 另外,我看到您使用函数而不是对象来执行操作。 Make sure you have redux-thunk middleware applied and there is no problem with your connect hoc in
Wrapper.js
确保您已应用了redux-thunk中间件,并且
Wrapper.js
connect hoc没有问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.