- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 87
Global state returned by useGlobal() does not subscribe to changes. #137
Comments
it works also: import { addReducer, setGlobal, useDispatch, useGlobal } from "reactn";
addReducer("increment", (global, dispatch) => {
console.log("increment", global.value);
return {
value: (global.value || 0) + 1
};
});
const Post = props => {
const increment = useDispatch("increment");
const [value] = useGlobal("value");
return (
<>
<Button
onClick={() => {
increment();
}}
>
{value}
</Button>
</>
);
}; |
@carvinlo I am facing the exact same problem. If i just subscribe to global state using However if I subscribe to a key using @CharlesStover is this the intended behaviour or are we missing something? |
I would be good to have a minimal reproduction on CodePen or something similar. I'd need to investigate if this is a problem with the reducer or a problem with I'd definitely like to solve it for you, but I don't see anything wrong with the code as I see it here, which is why a CodePen would be helpful. |
@CharlesStover Here is a stackblitz showing the issue: https://react-n7i5si.stackblitz.io/ Clicking on button will only update the component which has subscribed to an extra property other the globalstate. |
Thank you for that @ASHFAQPATWARI . I'm not familiar with stackbliz. Is it possible to see the original source code, similar to how CodePen lets you see the code alongside its output? |
I found the source code for stackblitz. Thank you, that is a good find. https://stackblitz.com/edit/react-n7i5si/ |
@CharlesStover any update on this one? |
I think this is due to the asynchronous nature of your lifecycle methods. Change As to why one component renders correctly and the other doesn't, I think that is just an implementation detail of React that should not be of concern. They both pass unit tests for re-rendering on state change. When dealing with proper lifecycle methods, this issue doesn't arise. When dealing with improper lifecycle methods, the resulting behavior need not be accounted for. Hope this helps. Great find! Without further reasoning, I believe this behavior is a side effect of the wrong lifecycle hook and that |
The text was updated successfully, but these errors were encountered: