Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export function useStore(store, { keys, deps = [store, keys] } = {}) {
: store.listen(emit(snapshotRef, onChange))
}, deps)
let get = () => snapshotRef.current
// `'init' in store` check for compatibility with nanostores <= 1.1.1
Copy link
Member

Choose a reason for hiding this comment

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

I think we can add ^1.2.0 to peer dependencies and avoid this check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. Would you like a PR to remove this check? I have the commit ready but you beat me to the version bump

Copy link
Member

Choose a reason for hiding this comment

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

I already fixed it and release :)

let init = 'init' in store ? () => store.init : get

return useSyncExternalStore(subscribe, get, get)
return useSyncExternalStore(subscribe, get, init)
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"index.js": "{ useStore }",
"nanostores": "{ map, computed }"
},
"limit": "916 B"
"limit": "932 B"
}
]
}
65 changes: 64 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import './setup.js'
import { act, render, screen } from '@testing-library/react'
import { delay } from 'nanodelay'
import { atom, computed, map, onMount, STORE_UNMOUNT_DELAY } from 'nanostores'
import { equal, notEqual } from 'node:assert'
import { deepStrictEqual, equal, notEqual } from 'node:assert'
import { afterEach, test } from 'node:test'
import type { FC, ReactNode } from 'react'
import React from 'react'
import { hydrateRoot } from 'react-dom/client'
import { renderToString } from 'react-dom/server'

import { useStore } from '../index.js'

Expand Down Expand Up @@ -242,3 +244,64 @@ test('useSyncExternalStore late subscription handling', () => {

equal(screen.getByTestId('subscription-test').textContent, 'updated content')
})

test('returns initial value until hydrated via useSyncExternalStore', t => {
type Value = 'new' | 'old'
let atomStore = atom<Value>('old')
let mapStore = map<{ value: Value }>({ value: 'old' })

let atomValues: Value[] = [] // Track values used across renders

let AtomTest: FC = () => {
let value = useStore(atomStore)
atomValues.push(value)
return h('div', { 'data-testid': 'atom-test' }, value)
}

let mapValues: Value[] = [] // Track values used across renders

let MapTest: FC = () => {
let value = useStore(mapStore).value
mapValues.push(value)
return h('div', { 'data-testid': 'map-test' }, value)
}

let Wrapper: FC = () => {
return h('div', { 'data-testid': 'test' }, h(AtomTest), h(MapTest))
}

// Create a "server" rendered element to re-hydrate. Thanks to childrentime
// https://github.com/testing-library/react-testing-library/issues/1120#issuecomment-2065733238
let ssrElement = document.createElement('div')
document.body.appendChild(ssrElement)
let html = renderToString(h(Wrapper))
ssrElement.innerHTML = html

equal(screen.getByTestId('atom-test').textContent, 'old')
equal(screen.getByTestId('map-test').textContent, 'old')

// Simulate store state change on client-side, after "server" render
atomStore.set('new')
mapStore.set({ value: 'new' })

// Hydrate into SSR element. Logs errors to console on hydration failure
let consoleErrorMock = t.mock.method(console, 'error', () => {})
act(() => {
hydrateRoot(ssrElement, h(Wrapper))
})

// Check nothing was logged to `console.error()`
let consoleErrorCall = consoleErrorMock.mock.calls[0] as
| { arguments: any }
| undefined
let consoleErrorMessage = String(consoleErrorCall?.arguments?.[0] ?? '')
equal(consoleErrorMessage, '')

// Confirm "server" render got old values, initial client render got old
// values at hydration, then post-hydration render got new values
deepStrictEqual(atomValues, ['old', 'old', 'new'])
deepStrictEqual(mapValues, ['old', 'old', 'new'])

equal(screen.getByTestId('atom-test').textContent, 'new')
equal(screen.getByTestId('map-test').textContent, 'new')
})
Loading