This repository was archived by the owner on Jun 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 223
feat: draft implementation of useStore in #733 (not production ready) #734
Open
billyrrr
wants to merge
3
commits into
yahoo:main
Choose a base branch
from
billyrrr:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import useFluxible from './useFluxible' | ||
|
|
||
| /** | ||
| * React hook that returns an executeAction handler | ||
| * TODO: this is a draft for an ongoing discussion in #733 | ||
| * | ||
| * Example: | ||
| * | ||
| * const FooComponent = () => { | ||
| * const executeAction = useExecuteAction(); | ||
| * return <p id={foo} onClick={() => excuteAction(...)} />; | ||
| * }; | ||
| * | ||
| * @function useExecuteAction | ||
| * @returns {Function} - executeAction handler | ||
| */ | ||
| const useExecuteAction = () => { | ||
| const { executeAction } = useFluxible(); | ||
| return executeAction; | ||
| } | ||
|
|
||
| export default useExecuteAction; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import {useEffect, useState, useLayoutEffect} from 'react'; | ||
| import useFluxible from './useFluxible' | ||
|
|
||
| /** | ||
| * React hook that returns a state from Fluxible store. | ||
| * TODO: this is a draft for an ongoing discussion in #733 | ||
| * | ||
| * Example: | ||
| * | ||
| * const FooComponent = () => { | ||
| * const foo = useStore('FooStore', store => store.getFoo()); | ||
| * return <p id={foo} />; | ||
| * }; | ||
| * | ||
| * @function useStore | ||
| * @returns {object} - a state from Fluxible store | ||
| */ | ||
| const useStore = (storeName, getStateFromStore) => { | ||
| const { getStore } = useFluxible(); | ||
| const store = getStore(storeName); | ||
| const [state, setState] = useState(getStateFromStore(store)); | ||
|
|
||
| function updateState() { | ||
| setState(getStateFromStore(store)); | ||
| } | ||
|
|
||
| // 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(() => { | ||
| store.on('change', updateState); | ||
| return () => store.removeListener('change', updateState); | ||
| }, []); | ||
|
|
||
| return state; | ||
| } | ||
|
|
||
| export default useStore; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import React, { useState } from 'react'; | ||
| import { expect } from 'chai'; | ||
| import TestRenderer from 'react-test-renderer'; | ||
| import createMockComponentContext from 'fluxible/utils/createMockComponentContext'; | ||
| import { useStore, FluxibleProvider, useExecuteAction, FluxibleComponent } from '../../../'; | ||
| import FooStore from '../../fixtures/stores/FooStore'; | ||
| import BarStore from '../../fixtures/stores/BarStore'; | ||
|
|
||
| const DumbComponent = () => { | ||
|
|
||
| const foo = useStore(FooStore, store => store.getFoo()); | ||
| const bar = useStore(BarStore, store => store.getBar()); | ||
| const executeAction = useExecuteAction(); | ||
| const onClick = () => executeAction((context) => context.dispatch('DOUBLE_UP')) | ||
|
|
||
| return ( | ||
| <div> | ||
| <span id="foo">{foo}</span> | ||
| <span id="bar">{bar}</span> | ||
| <button id="button" onClick={onClick} /> | ||
| </div> | ||
| ) | ||
| }; | ||
|
|
||
| DumbComponent.displayName = 'DumbComponent'; | ||
| DumbComponent.initAction = () => {}; | ||
|
|
||
| const stores = [FooStore, BarStore]; | ||
|
|
||
| const renderComponent = (Component) => { | ||
| const context = createMockComponentContext({ stores }); | ||
|
|
||
| const app = TestRenderer.create( | ||
| <FluxibleComponent context={context}> | ||
| <Component ref={undefined} /> | ||
| </FluxibleComponent> | ||
| ); | ||
|
|
||
| return { app, context }; | ||
| }; | ||
|
|
||
| describe('fluxible-addons-react', () => { | ||
| describe('useStore', () => { | ||
| it('returns fluxible store', () => { | ||
| const FooComponent = () => { | ||
| const foo = useStore('FooStore', store => store.getFoo()) | ||
| return <p id={foo} />; | ||
| }; | ||
|
|
||
| const context = createMockComponentContext({ stores: [FooStore] }); | ||
|
|
||
| const testRenderer = TestRenderer.create( | ||
| <FluxibleProvider context={context}> | ||
| <FooComponent /> | ||
| </FluxibleProvider> | ||
| ); | ||
|
|
||
| const component = testRenderer.root.findByType('p'); | ||
|
|
||
| expect(component.props.id).to.deep.equal('bar'); | ||
| }); | ||
| it('should register/unregister from stores on mount/unmount', () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| const { app, context } = renderComponent(DumbComponent); | ||
|
|
||
| const barStore = context.getStore(BarStore); | ||
| const fooStore = context.getStore(FooStore); | ||
|
|
||
| expect(barStore.listeners('change').length).to.equal(1); | ||
| expect(fooStore.listeners('change').length).to.equal(1); | ||
|
|
||
| app.unmount(); | ||
|
|
||
| expect(barStore.listeners('change').length).to.equal(0); | ||
| expect(fooStore.listeners('change').length).to.equal(0); | ||
| }); | ||
| it('should listen to store changes', () => { | ||
| const { app } = renderComponent(DumbComponent); | ||
| const button = app.root.findByProps({ id: 'button' }) | ||
| const foo = app.root.findByProps({ id: 'foo' }) | ||
| const bar = app.root.findByProps({ id: 'bar' }) | ||
|
|
||
| button.props.onClick(); | ||
| console.error(foo) | ||
|
|
||
| expect(foo.props.children).to.equal('barbar'); | ||
| expect(bar.props.children).to.equal('bazbaz'); | ||
| }); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
This causes the assertions in 2 to fail.
With useLayoutEffect:
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.
This is how they do: https://github.com/reduxjs/react-redux/blob/adade2069061e09eaa046b10389aee265048e034/src/components/connect.tsx#L56