-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issues with hooks in useStoryblok #112
Comments
i also think it is missleading to use the "useStoryblokBridge" name inside storyblok-react lib because as far i can see it is simply a re-export from @storyblok/js which has nothing to do with React: Line 21 in 56a8adf
if a user directly uses it as if it was a React hook (at the root of the render func) the event will gets added again and again, render after render. i think you should implement a proper React hook in this lib (wrapping the call to useStoryblokBridge in a useEffect with a the callback, etc.. as dependencies + do the needed cleaning up as johannes-lindgren mentionned) or at least you should make it very clear in the doc that useStoryblokBridge is NOT a hook as its name suggests. |
I have a similar issue with This storyblok-react/lib/common/client.ts Line 22 in 60cd8de
I would really suggest using React's eslint plugin to enforce the Rules of Hooks. |
I think the first issue is the most annoying as it causes the following error with each HMR update: I just noticed it in my Next js application and found it pretty hard to narrow down. From my understanding this could be easily resolved by moving the conditional to the |
I just created a PR for this #870. I'd appreciate if someone could have a look at it :) |
Hello 👋! Right now the
When trying to edit something this can be observed in the live editor by content jumping around when something is changed, because the change is only displayed for one render. This can be solved by pulling const useStoryblokState: TUseStoryblokState = (initialStory = null, bridgeOptions = {}) => {
const [story, setStory] = useState(initialStory)
const isBridgeEnable = typeof window !== 'undefined' && typeof window.storyblokRegisterEvent !== 'undefined'
const id = (story as any).internalId || story.id
useEffect(() => {
if (!isBridgeEnable || !initialStory) return
setStory(initialStory)
registerStoryblokBridge(id, (newStory) => setStory(newStory), bridgeOptions)
}, [initialStory, isBridgeEnable, id])
if (!isBridgeEnable || !initialStory) {
return initialStory
}
return story
} If you would want to adhere to the rules of hooks completely, including exhaustive-deps, you would have to include Overall I don't think it is necessary here to be that strict with the dependencies in general. The function |
@whlufisc sounds good to me. Can you prepare a pull request? |
Someone else was faster #883 |
Hello, is there anyone (maybe @fgiuliani ) who could do a review of the PR #883 ? The non-functioning preview is unsettling our editors and annoying us, as we cannot provide any other solution to the problem at the moment. Thanks in advance! |
Hey @franzi-wtfoxtrot @whlufisc - The PR was merged a couple of weeks back :) Maybe we can close this issue now @johannes-lindgren ? |
There are a few issues with the
useStoryblok()
:useEffect()
is called conditionally on line 40, caused by early return statement on line 35. See the rules of hooksuseSbBridge()
causes side-effects, yet it is not called within `useEffect(). (Despite its name, useSbBridge is not a react hook)useStoryblokBridge()
cause side-effects that are not cleaned up. How do we prevent memory leaks? What if the registered events try to mutate the state of a component that has unmounted?setState()
is called. See https://www.codingdeft.com/posts/react-prevent-state-update-unmounted-component/useStoryblokBridge()
, we are subscribing to events from the bridge. If two different components are callinguseStoryblokBridge()
, then I assume that line 37 will be called twice; one will pass the condition and result in a mutated state, and the other will cause the window to reload. That's a side-effect that is not easy to foresee. Since we are not cleaning up the event listeners (I think), anytime we unmount the story component and replace it with another story component, the window will reload.The text was updated successfully, but these errors were encountered: