Cha*_*man 3 javascript reducers reactjs redux react-redux
目前在我的 React-Redux 应用程序中,我想使用 React 将显示状态设置为 false 或 true。当设置为 true 时,应用程序将初始化。(有多个组件,因此使用 react/redux 执行此操作是有意义的。)
但是,我当前的问题是,即使我使用 react redux 连接了我的应用程序,并且使用提供程序连接了我的商店,也会调用调度操作,而不更新商店(我正在使用 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);
Run Code Online (Sandbox Code Playgroud)
行动
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
};
}
Run Code Online (Sandbox Code Playgroud)
减速器
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;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢,再次非常感谢您的帮助。
所以,我向一位同事寻求帮助,结果我将我的操作作为对象而不是函数返回。因此,例如,更改以下代码:
hideBlock(){
const{dispatch} = this.props;
dispatch(hideBlock);
}
Run Code Online (Sandbox Code Playgroud)
到
hideBlock(){
const{dispatch} = this.props;
//change hideBlock to hideBlock()
dispatch(hideBlock());
}
Run Code Online (Sandbox Code Playgroud)
解决了这个问题。谢谢安德鲁!
| 归档时间: |
|
| 查看次数: |
4146 次 |
| 最近记录: |