Skip to content
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

Fix/useStore #431

Merged
merged 6 commits into from Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/react/__tests__/useStore.test.tsx
Expand Up @@ -90,6 +90,29 @@ describe('useStore', () => {
]
`)
})
it('should correct work, when store contains function', async () => {
const fn = jest.fn()
const changeStore = createEvent()
const $store = createStore(() => 0).on(changeStore, (_, p) => p)

const Display = () => {
const store = useStore($store)
fn()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a proof that issues does exists at all: your example reveal that current implementation works as expected

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return store()
}

await render(<Display />)

expect(container.firstChild).toMatchInlineSnapshot(`0`)
expect(fn).toHaveBeenCalledTimes(1)

await act(async () => {
changeStore(() => 1)
})

expect(container.firstChild).toMatchInlineSnapshot(`1`)
expect(fn).toHaveBeenCalledTimes(2)
})
it('should subscribe before any react hook will change store', async () => {
const fn = jest.fn()
const fx = createEffect({
Expand Down
2 changes: 1 addition & 1 deletion src/react/useStore.ts
Expand Up @@ -6,7 +6,7 @@ import {throwError} from './throw'
export function useStore<State>(store: Store<State>): State {
if (!is.store(store)) throwError('expect useStore argument to be a store')
const currentStore = React.useRef(store)
const setState = React.useState(store.getState())[1]
const setState = React.useReducer((_, action) => action, store.getState())[1];
useIsomorphicLayoutEffect(() => {
if (currentStore.current === store) {
setState(store.getState())
Expand Down