属性 触发 redux dispatch 后不更新
Property doesn't update after firing redux dispatch
下班后我不得不寻求帮助,我的 useEffect 并不总是适用于 reduxs 调度操作。
const ScreenA = { currentItem, updatedItem } => {
useEffect(() => {
if (currentItem.id === updatedItem.id) { // Do stuff }
}, [updatedItem]) // XX Is not changing
... // do stuf
const mapStateToProps = (state) => {
console.log("AAABB ScreenA.mapStateToProps updatedItem: ", state.updatedItem) // XX I receive new updated id!! but it doesn't change the prop.updatedItem for ScreenA and useEffect is not calling.
return {
updatedItem: state.updatedItem,
}
}
export default connect(mapStateToProps)(FeedScreen)
}
我的减速机
更新
const initialState = {
updatedItem: undefined,
}
export default (state = initialState, action) => {
switch (action.type) {
case ITEM_CHANGED:
console.log("AAABB reducer.addChangedItem.ITEM_CHANGED: " + action.t) // Is printing after every dispatch call
return {
...state,
updatedItem: action.updatedItem,
}
// I tried also with
return {
updatedItem: action.updatedItem,
}
default:
return state
}
}
奇怪的是,如果我将更改操作从 ScreenB 分派到 ScreenA 而不是在 ScreenC 到 ScreenA 之间,它会起作用。 (这与我发送操作的方式相同。
也将更新后的物品展开
因为您更新的项目是一个可能会导致突变的对象。似乎不太可能,但这是您在不使用平面数据时面临的问题。通常,您在 redux 中的操作中还有一个 type
和一个 payload
(未更新的项目)。
return {
...state,
updatedItem: {...action.updatedItem},
}
如果这不起作用,则可能是您的 useEffect 中的某些部分已损坏,您需要在那里登录并显示结果。
下班后我不得不寻求帮助,我的 useEffect 并不总是适用于 reduxs 调度操作。
const ScreenA = { currentItem, updatedItem } => {
useEffect(() => {
if (currentItem.id === updatedItem.id) { // Do stuff }
}, [updatedItem]) // XX Is not changing
... // do stuf
const mapStateToProps = (state) => {
console.log("AAABB ScreenA.mapStateToProps updatedItem: ", state.updatedItem) // XX I receive new updated id!! but it doesn't change the prop.updatedItem for ScreenA and useEffect is not calling.
return {
updatedItem: state.updatedItem,
}
}
export default connect(mapStateToProps)(FeedScreen)
}
我的减速机 更新
const initialState = {
updatedItem: undefined,
}
export default (state = initialState, action) => {
switch (action.type) {
case ITEM_CHANGED:
console.log("AAABB reducer.addChangedItem.ITEM_CHANGED: " + action.t) // Is printing after every dispatch call
return {
...state,
updatedItem: action.updatedItem,
}
// I tried also with
return {
updatedItem: action.updatedItem,
}
default:
return state
}
}
奇怪的是,如果我将更改操作从 ScreenB 分派到 ScreenA 而不是在 ScreenC 到 ScreenA 之间,它会起作用。 (这与我发送操作的方式相同。
也将更新后的物品展开
因为您更新的项目是一个可能会导致突变的对象。似乎不太可能,但这是您在不使用平面数据时面临的问题。通常,您在 redux 中的操作中还有一个 type
和一个 payload
(未更新的项目)。
return {
...state,
updatedItem: {...action.updatedItem},
}
如果这不起作用,则可能是您的 useEffect 中的某些部分已损坏,您需要在那里登录并显示结果。