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

feat: use React 19 use in useStore api #2755

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 15 additions & 3 deletions src/react/Provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { createContext, createElement, useContext, useRef } from 'react'
import type { FunctionComponentElement, ReactNode } from 'react'
import ReactExports, {
type Context,
type FunctionComponentElement,
type ReactNode,
createContext,
createElement,
useContext,
useRef,
} from 'react'
import { createStore, getDefaultStore } from '../vanilla.ts'

type Store = ReturnType<typeof createStore>
Expand All @@ -13,8 +20,13 @@ type Options = {
store?: Store
}

// In React 19, `use` will allow calling within conditionals and loops.
// Refs: https://19.react.dev/reference/react/use#reading-context-with-use
Comment on lines +23 to +24
Copy link
Member

Choose a reason for hiding this comment

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

But it doesn't allow calling useStore in a condition. This is pretty unfortunate, but can't be helped.

So, I don't think this PR helps. It rather misleads to misusage.
If you want to use use, the current recommendation is to use custom context on your end.

const hook: <T>(context: Context<T>) => T =
'use' in ReactExports ? ReactExports.use : useContext

export const useStore = (options?: Options): Store => {
const store = useContext(StoreContext)
const store = hook(StoreContext)
return options?.store || store || getDefaultStore()
}

Expand Down