feat: draft implementation of useStore in #733 (not production ready)#734
feat: draft implementation of useStore in #733 (not production ready)#734billyrrr wants to merge 3 commits intoyahoo:mainfrom
Conversation
…eady) Co-authored-by: pablopalacios <pablo.palacios@holidaycheck.com>
|
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.
Why do we need to use useLayoutEffect and not just useEffect?
There was a problem hiding this comment.
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.
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.
|
|
||
| expect(component.props.id).to.deep.equal('bar'); | ||
| }); | ||
| it('should register/unregister from stores on mount/unmount', () => { |
There was a problem hiding this comment.
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.
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 pablo.palacios@holidaycheck.com
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.