gpt4 book ai didi

javascript - 调度 Action 触发,但 redux 存储不更新

转载 作者:行者123 更新时间:2023-11-29 10:36:12 24 4
gpt4 key购买 nike

目前在我的 React-Redux 应用程序中,我想使用 React 将显示状态设置为 false 或 true。设置为 true 时,应用程序将初始化。 (有多个组件,所以使用 react/redux 来做这件事是有意义的。)

但是,我当前的问题是,即使我已经使用 react redux 连接了我的应用程序,并且我的商店使用了 provider,但仍会调用分派(dispatch)操作,而不更新商店(我正在使用 redux 开发工具进行双重检查,因为以及应用测试)。

我附上了我认为相关的代码,但是,整个代码库可作为专门针对此问题制作的分支提供here .我在这方面花了很多时间(实际上是轻描淡写),任何贡献都将不胜感激。

组件相关代码

hideBlock(){
const{dispatch} = this.props;
dispatch(hideBlock);
}

return(
<div className = "works">
<button id="show-block" type="button" className="show-hide-button" onClick={this.showBlock}>show</button>
<button id="hide-block" type="button" className="show-hide-button" onClick={this.hideBlock}>Hide</button>
</div>
);

function mapStateToProps(state) {
const {environment} = state;
return{
environment
}
}

export default connect(mapStateToProps)(Form);

Action

import * as types from "../constants/ActionTypes";

export function showBlock(show) {
return {
type: types.SHOW,
show: true
};
}

export function hideBlock(hide) {
return {
type: types.HIDE,
show: false
};
}

reducer

import * as types from "../constants/ActionTypes";

const initialState = {
show: false
};

export default function environment(state = initialState, action) {
switch(action.type) {
case types.HIDE:
return Object.assign({}, state, {
type: types.HIDE
});
case types.SHOW:
return Object.assign({}, state, {
type: types.SHOW
});
default:
return state;
}
}

谢谢,再次非常感谢您的帮助。

最佳答案

因此,我向一位同事寻求帮助,结果发现我将我的操作作为对象而不是函数返回。因此,例如,更改以下代码:

hideBlock(){
const{dispatch} = this.props;
dispatch(hideBlock);
}

hideBlock(){
const{dispatch} = this.props;
//change hideBlock to hideBlock()
dispatch(hideBlock());
}

解决了这个问题。谢谢安德鲁!

关于javascript - 调度 Action 触发,但 redux 存储不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36003616/

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