Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 组件中的Redux状态在操作分派后未更新,但显示在Redux devtools中_Javascript_Reactjs_Redux_React Redux - Fatal编程技术网

Javascript 组件中的Redux状态在操作分派后未更新,但显示在Redux devtools中

Javascript 组件中的Redux状态在操作分派后未更新,但显示在Redux devtools中,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,单击菜单中的某个项目时,我正在运行这段代码 从“React”导入React,{Component}; 导入“react下拉树select/dist/styles.css”; 从'react redux'导入{connect}; 导入“../../css/tree.css”; 从“../../../redux/actions/menu/updateSelectedTree”导入updateSelectedTree; 从“../../../redux/actions/plot/traceActio

单击菜单中的某个项目时,我正在运行这段代码


从“React”导入React,{Component};
导入“react下拉树select/dist/styles.css”;
从'react redux'导入{connect};
导入“../../css/tree.css”;
从“../../../redux/actions/menu/updateSelectedTree”导入updateSelectedTree;
从“../../../redux/actions/plot/traceActions”导入{updateTraceName};
从“../../../../redux/actions/menu/renameActions”导入{removeNameProp,pushRenameProp}”
从“../../../utils/resolveTraceName”导入resolveTraceName;
从“/MeasureTreeSelector”导入MeasureTreeSelector;
类MeasureTreeSelectorContainer扩展组件{
render(){
返回();
}
handleTreeChange=(当前,选中)=>{
console.log(当前)
//获取所选项目的顺序
const selectedProperty=[…curr.path.split(“/”).splice(1),curr.value]
如果(当前选中){
this.props.pushRenamePro(selectedProperty)
}否则{
this.props.removeNameProp(selectedProperty)
}
console.log(this.props.rename_props)
this.props.updateSelectedTree(this.props.rename\u props)
}
updateTraces=()=>{
this.props.measures.forEach(元素=>{
const newName=resolveTraceName(元素,this.props.rename_props);
console.log(newName)
this.props.updateTraceName(newName,element.id)
}) 
}
}
常量mapStateToProps=(状态)=>{
console.log(state.menuReducer)
返回{
标记:state.menuReducer.tags,
选中:state.menuReducer.selected,
度量值:state.menuReducer.json,
重命名_道具:state.menuReducer.rename_道具
}
}
const mapDispatchToProps={
更新选定树,
updateTraceName,
移除eMap,
推顶
}
导出默认连接(mapStateToProps、mapDispatchToProps)(MeasureTreeSelectorContainer);
pushRenameProp和RemoveEnameProp是发送的同步操作,用于更新rename_道具。这些减缩器不会改变状态,而是创建一个新的状态,这意味着组件中应该有一个更新


导出函数pushRenameProp(prop){
返回{
类型:'PUSH_RENAME_PROP',
有效载荷:道具
}
}
导出函数删除器ENAMEPROP(prop){
返回{
类型:'REMOVE\u RENAME\u PROP',
有效载荷:道具
}
}
案例“PUSH\u RENAME\u PROP”:{
console.log(action.payload)
返回{
……国家,
重命名_道具:[…状态。重命名_道具,操作。有效负载]
}
}
案例“删除\重命名\道具”:{
返回{
……国家,
重命名_props:state.rename_props.filter((e,i)=>{
返回JSON.stringify(e)!=JSON.stringify(action.payload)
})
}
}
执行此操作时,devtools显示redux状态已更新,但代码显示rename_props为空


组件道具似乎是redux状态后面的一个状态更改

此行
This.props.updateSelectedTree(This.props.rename_props)
重命名道具设置为在设置它们之前打开的状态。
所以它只是否定了这种改变

你可以在一声巨响中做到这一点:

export function pushRenameProp(prop) {
    return function (dispatch, getState) {
        dispatch({
            type:'PUSH_RENAME_PROP',
            payload: prop
        })
        const state = getState();
        
        return dispatch(updateSelectedTree(state.menuReducer.rename_props)
    }
}
确保
updateSelectedTree
具有最新状态

*代码没有经过测试,但我认为您已经了解了其中的要点

但是,也可能根本不需要
updateSelectedTree

您只需按/删除即可更改状态,为什么要更新?

您可以共享完整的相关代码吗?@tmhao2005还需要哪些代码?完整的组件和操作。或者您可以设置代码沙盒吗?我无法设置代码沙盒,但是我已将完整的组件代码和操作添加到postI use updateSelectedTree,以使UI组件由redux状态控制,并且不会影响重命名道具。当我去掉updateSelectedTree时,我仍然存在与before@Darruma您还可以发布
updateSelectedTree
code吗?我不明白你的意思,连接是用来控制组件的redux状态,
updateSelectedTree
是一个动作,不是吗?就像推/卸一样。。状态通过操作更改,连接订阅到状态更改。并约束行动创造者