您需要添加
reduxState.message
和
localIndicator
到useEffect的dependencies数组,以便它知道在发生更改时进行更新。当前,useEffect将在每个渲染周期上运行,这并不理想:
useEffect( () => {
/* After I click the form, the local indicator updates to true
so the message is updated. THE ISSUE IS the reduxState has not yet been updated!
By the time it updates, this has already happened and so im displaying the old message
not the new one
*/
if (setLocalIndicator === true){
setMessage(reduxState.message)
setLocalIndicator(false) // to prevent infinite re-renders
}
},[localIndicator, reduxState.message])