使用逗号运算符在 redux 工具包中的同一个调度中调用多个 reducer 不起作用
Calling multiple reducers inside the same dispatch in redux toolkit with the comma operator doesn't work
根据 MDN:
The comma operator (,) evaluates each of its operands (from left to right) and returns the value of the last operand.
This lets you create a compound expression in which multiple expressions are evaluated,
with the compound expression's final value being the value of the rightmost of its member expressions.
This is commonly used to provide multiple parameters to a for loop
所以我认为这样的事情会奏效:
const gameData = useDispatch();
gameData((
readGameAnswers(props.gameAnswers),
readGameAnswersImage(props.gameAnswersImages as string[]),
readCorrectAnswer(props.correctAnswer)));
(其中 3 个读取函数是在商店中定义的 reducer))
尽管该行为不是我所期望的 - 仅调用了最后一次读取。为什么会这样?根据逗号运算符的定义,它不应该工作吗?
不幸的是,不,那行不通,因为每个方法调用只提供一个操作,并且由于逗号的行为,只有最后一个返回给调度程序 (gameData
)。您正在寻找的是一个将多个 reducer 调用合并为一个的 action creator。
我相信 有您想要的答案!
根据 MDN:
The comma operator (,) evaluates each of its operands (from left to right) and returns the value of the last operand.
This lets you create a compound expression in which multiple expressions are evaluated,
with the compound expression's final value being the value of the rightmost of its member expressions.
This is commonly used to provide multiple parameters to a for loop
所以我认为这样的事情会奏效:
const gameData = useDispatch();
gameData((
readGameAnswers(props.gameAnswers),
readGameAnswersImage(props.gameAnswersImages as string[]),
readCorrectAnswer(props.correctAnswer)));
(其中 3 个读取函数是在商店中定义的 reducer))
尽管该行为不是我所期望的 - 仅调用了最后一次读取。为什么会这样?根据逗号运算符的定义,它不应该工作吗?
不幸的是,不,那行不通,因为每个方法调用只提供一个操作,并且由于逗号的行为,只有最后一个返回给调度程序 (gameData
)。您正在寻找的是一个将多个 reducer 调用合并为一个的 action creator。
我相信