-
Notifications
You must be signed in to change notification settings - Fork 227
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
feat: draft implementation of useStore in #733 (not production ready) #734
base: main
Are you sure you want to change the base?
Conversation
…eady) Co-authored-by: pablopalacios <[email protected]>
Cool, let's go step by step. Important tests that we should have:
You can take a look at |
// useLayoutEffect is the closest to componentDidMount | ||
// (we want to block render until store is subscribed) | ||
// TODO: NOTE useLayoutEffect is called on server-side during SSR | ||
useLayoutEffect(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to use useLayoutEffect and not just useEffect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original test would fail for useEffect. I think that the following would take place with useEffect:
- render and return
- Test runs assertions on the return
- Actions in useEffect is triggered
This causes the assertions in 2 to fail.
With useLayoutEffect:
- Render
- Actions in useLayoutEffect is executed and waited to be finished
- Test runs assertions on the return
Basically, it is just to ensure the same behavior as connectToStore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. Could you please take a look how react-redux is handling this issue: https://blog.isquaredsoftware.com/2018/11/react-redux-history-implementation/#connect-is-implemented-using-hooks
They are also using useLayoutEffect but on SSR they use useEffect to prevent the warning message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
expect(component.props.id).to.deep.equal('bar'); | ||
}); | ||
it('should register/unregister from stores on mount/unmount', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to make it simple to navigate between code blocks, I would recommend you to add one blank line between each test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I can do that. The IDE handles most of the reformatting though, so I might approach it by adding a lint rule.
@billyrrr @pablopalacios Still interested in this feature? |
@redonkulus I do. At work we are using 2 custom hooks for stores: This is an example on how we use it: function MyComponent() {
const { locale } = useStore(AppStore).getState();
const { count } = useStoreSubscription(CountStore).getState();
return <span>Count: {new Intl.NumberFormat(locale).format(count)}</span>;
} We have been using these hooks in production successfully since probably the time this PR was created. @billyrrr do you still have interested in moving the PR forward, otherwise I can prepare another one. |
Yep, I can continue to work on this issue. |
Co-authored-by: pablopalacios [email protected]
I confirm that this contribution is made under the terms of the license found in the root directory of this repository's source tree and that I have the authority necessary to make this contribution on behalf of its copyright owner.