-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
#182 SSR data is shared #375
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3e3d6df
context utils for SSR
Munawwar 40c42c0
move useIsomorphicLayoutEffect to new file
Munawwar 7029076
added build commands
Munawwar 47d828d
1. remove the useEffect we dont need anymore 2. wrap context and hook…
Munawwar 9f81d24
issue #182 - changed name to createContext and useStore + added tests
Munawwar a71cb5d
remove default Provider
Munawwar d9a4586
use alias to index file
Munawwar db2be8b
change 'createState' prop to 'initialStore'
Munawwar 8566396
updated tests
Munawwar 1bd9110
code review changes
Munawwar 149703c
snapshot update
Munawwar bc7c75c
Merge branch 'master' into issue-182-ssr
dai-shi d24d146
add a section in readme
dai-shi 3d882e3
chore imports and so on
dai-shi 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
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,59 @@ | ||
import React, { | ||
createContext as reactCreateContext, | ||
useContext, | ||
useRef, | ||
} from 'react' | ||
import create, { UseStore } from 'zustand' | ||
import { EqualityChecker, State, StateCreator, StateSelector } from './vanilla' | ||
|
||
function createContext<TState extends State>(initialState?: TState) { | ||
const ZustandContext = reactCreateContext<UseStore<TState> | undefined>( | ||
undefined | ||
) | ||
|
||
const Provider = ({ | ||
createState, | ||
children, | ||
}: { | ||
createState: StateCreator<TState> | ||
children: React.ReactNode | ||
}) => { | ||
const storeRef = useRef<UseStore<TState>>() | ||
|
||
if (!storeRef.current) { | ||
storeRef.current = create(createState) | ||
} | ||
|
||
return React.createElement( | ||
dai-shi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ZustandContext.Provider, | ||
{ value: storeRef.current }, | ||
children | ||
) | ||
} | ||
|
||
const useStore = <StateSlice>( | ||
selector?: StateSelector<TState, StateSlice>, | ||
equalityFn: EqualityChecker<StateSlice> = Object.is | ||
) => { | ||
// ZustandContext value is guaranteed to be stable. | ||
const useProviderStore = useContext(ZustandContext) | ||
if (!useProviderStore) { | ||
throw new Error( | ||
'Seems like you have not used zustand provider as an ancestor.' | ||
) | ||
} | ||
return useProviderStore( | ||
// FIXME: this type assertion seems unnecessary, as useStore does accept undefined. | ||
Munawwar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Need to fix useStore()'s types | ||
selector as StateSelector<TState, StateSlice>, | ||
equalityFn | ||
) | ||
} | ||
|
||
return { | ||
Provider, | ||
useStore, | ||
} | ||
} | ||
|
||
export default createContext |
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,68 @@ | ||
import React from 'react' | ||
import { cleanup, render } from '@testing-library/react' | ||
import { StateCreator } from '../src/index' | ||
import createContext from '../src/context' | ||
|
||
const consoleError = console.error | ||
afterEach(() => { | ||
cleanup() | ||
console.error = consoleError | ||
}) | ||
|
||
type CounterState = { | ||
count: number | ||
inc: () => void | ||
} | ||
|
||
it('creates and uses context store', async () => { | ||
const { Provider, useStore } = createContext<CounterState>() | ||
|
||
const createParam: StateCreator<CounterState> = (set) => ({ | ||
count: 0, | ||
inc: () => set((state) => ({ count: state.count + 1 })), | ||
}) | ||
|
||
function Counter() { | ||
const { count, inc } = useStore() | ||
React.useEffect(inc, [inc]) | ||
return <div>count: {count}</div> | ||
Munawwar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
const { findByText } = render( | ||
<Provider createState={createParam}> | ||
<Counter /> | ||
</Provider> | ||
) | ||
|
||
await findByText('count: 1') | ||
}) | ||
|
||
Munawwar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
it('throws error when not using provider', async () => { | ||
console.error = jest.fn() | ||
|
||
class ErrorBoundary extends React.Component<{}, { hasError: boolean }> { | ||
constructor(props: {}) { | ||
super(props) | ||
this.state = { hasError: false } | ||
} | ||
static getDerivedStateFromError() { | ||
return { hasError: true } | ||
} | ||
render() { | ||
return this.state.hasError ? <div>errored</div> : this.props.children | ||
} | ||
} | ||
|
||
const { useStore } = createContext<CounterState>() | ||
function Component() { | ||
useStore() | ||
return <div>no error</div> | ||
} | ||
|
||
const { findByText } = render( | ||
<ErrorBoundary> | ||
<Component /> | ||
</ErrorBoundary> | ||
) | ||
await findByText('errored') | ||
}) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.