Javascript 我的Redux商店更新了,组件中的道具也被更新了,但是组件没有重新发布
我有一个Redux,在我规范化数据之前它工作得很好,现在我有2个数组作为存储 写了另一个减缩器,把它们组合起来 当我调度一个动作时,减速机工作正常,并返回正确的新值 商店是updatet,子组件以及应用程序容器组件中的道具也是updatet。 但是组件无法识别道具更改。(他们不会进入willReceiveProps) 但是在google Chrome的react开发者工具中,这些值是正确的 我的减速机:Javascript 我的Redux商店更新了,组件中的道具也被更新了,但是组件没有重新发布,javascript,reactjs,typescript,redux,react-redux,Javascript,Reactjs,Typescript,Redux,React Redux,我有一个Redux,在我规范化数据之前它工作得很好,现在我有2个数组作为存储 写了另一个减缩器,把它们组合起来 当我调度一个动作时,减速机工作正常,并返回正确的新值 商店是updatet,子组件以及应用程序容器组件中的道具也是updatet。 但是组件无法识别道具更改。(他们不会进入willReceiveProps) 但是在google Chrome的react开发者工具中,这些值是正确的 我的减速机: `export default handleActions<any, NodeMode
`export default handleActions<any, NodeModel>({
[ADD_NODE]: (state: any, action: Action<NodeModel>): any => {
let result = [...state, action.payload];
return result;
},
[MOVE_NODE]: (state: any, action: Action<NodeModel>): any => {
let result = assign(state)
Object.keys(result).map((n: any) => {
result[n].id == action.payload.id ?
result[n] = action.payload : result[n] = result[n]
})
return result;
}
`export default handleActions({
[添加节点]:(状态:任意,操作:操作):任意=>{
让result=[…状态,action.payload];
返回结果;
},
[移动节点]:(状态:任意,操作:操作):任意=>{
让结果=分配(状态)
Object.keys(result.map)(n:any)=>{
结果[n].id==action.payload.id?
结果[n]=操作。有效负载:结果[n]=结果[n]
})
返回结果;
}
},normalizedStoreData.entities.Nodes)`
我的行动:
export const addNode = createAction<NodeModel, NodeModel>(
ADD_NODE,
(node : NodeModel) => node
);
export const moveNode = createAction<NodeModel, NodeModel, number>(
MOVE_NODE,
(node : NodeModel, newParentId : number) => {
let result : NodeModel = assign(node, { ParentId : newParentId})
return result;
}
);
export const addNode=createAction(
添加_节点,
(节点:NodeModel)=>节点
);
export const moveNode=createAction(
移动_节点,
(节点:NodeModel,newParentId:number)=>{
let结果:NodeModel=assign(节点,{ParentId:newParentId})
返回结果;
}
);
App.tsx(应用程序容器):
createColumn(列:任意,分派:任意,键?:任意){
返回(
调度(移动节点(t,s))}
addNode={(t:any)=>dispatch(addNode(t))}
Nodes={getNodes(this.state.data,Col.id)}
列={Col}/>);
}
createColumns(分派:任意){
let结果:任意[]=[];
this.state.data.Columns.map((Col:any)=>{
push(this.createColumn(Col,dispatch,Col.id));
})
返回结果;
}
组件将接收道具(下一步:任何){
console.log(nextrops);
这个.forceUpdate();
}
shouldComponentUpdate(nextrops:any,nextState:any){
控制台日志(“EDFDF”);
这个.forceUpdate();
}
render(){
const dispatch=this.props.dispatch;
const data=this.state.data;
返回(
{this.createColumns(dispatch)}
)
}
看起来您的组件没有正确连接到redux存储,这个.state.data从哪里来?不要使用this.state
,你应该将存储数据映射到props
,而shoulldcomponentupdate
将比较props
和nextrops
来决定是否需要更新。很抱歉,我以前做过测试,正如你所说,我使用了这些props。但是我重新设置了它,问题仍然和以前一样const-mapStateToProps=(state:any)=>{console.log(state);return{data:state}}}导出默认连接(mapstatetops)(DragDropContext(HTML5Backend)(App))
mapstatetops
将存储状态映射到this.props.data
,而不是this.state.data
。mapstatetops
中的state
是整个应用程序状态,组件中的this.state
是本地状态,它们是不同的。是的,我知道,会发生这种情况。为了进行测试,我将道具置于组件状态。但是我改回来了,现在使用道具,但是视图仍然没有更新。是的,谢谢!!将减速机方法更改为此,结果成功<代码>[MOVE_NODE]:(state:any,action:action):any=>{let result=[…state];Object.keys(result).map((n:any)=>{result[n].id==action.payload.id?result[n]=action.payload:result[n]=result[n]})
createColumn(Col: any, dispatch: any, key?: any) {
return (
<Column
key={key}
moveNode={(t: any, s: any) => dispatch(moveNode(t, s))}
addNode={(t: any) => dispatch(addNode(t))}
Nodes={getNodes(this.state.data, Col.id)}
Column={Col} />);
}
createColumns(dispatch: any ){
let result : any[] = [];
this.state.data.Columns.map((Col : any) => {
result.push(this.createColumn(Col, dispatch, Col.id));
})
return result;
}
componentWillReceiveProps(nextProps : any){
console.log(nextProps);
this.forceUpdate();
}
shouldComponentUpdate(nextProps : any, nextState : any){
console.log("EDFDF");
this.forceUpdate();
}
render() {
const dispatch = this.props.dispatch;
const data = this.state.data;
return (
<div className="container">
{this.createColumns(dispatch)}
</div>
)
}