-
Notifications
You must be signed in to change notification settings - Fork 181
Add ClientProvider, useClient, and useClientCapability
#1607
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
Open
mcintyre94
wants to merge
1
commit into
main
Choose a base branch
from
react/provider
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| '@solana/react': minor | ||
| '@solana/errors': minor | ||
| --- | ||
|
|
||
| Add `ClientProvider`, `useClient`, and `useClientCapability` — the Kit client context layer for React. | ||
|
|
||
| `ClientProvider` publishes a caller-owned Kit client to its subtree. Required by `useClient`, `useClientCapability`, and any plugin-specific hook that depends on a client capability — generic primitives like `useAction` work against arbitrary async functions and don't need a provider. The provider accepts both synchronous clients and promise-returning ones — when given a promise (e.g. `createClient().use(asyncPlugin())`), it suspends via the nearest `<Suspense>` boundary until the client resolves. On React 19 it delegates to `React.use(promise)`; on React 18 an internal thrown-promise shim, keyed by promise identity, honours the same contract. | ||
|
|
||
| `useClient<TClient>()` is the basic context accessor. Defaults to the base `Client` shape; callers who know a specific plugin is installed may widen the type via the generic. Throws a new `SolanaError` with code `SOLANA_ERROR__REACT__MISSING_PROVIDER` when called outside a provider. | ||
|
|
||
| `useClientCapability<TClient>({ capability, hookName, providerHint })` runtime-checks that the requested capability (or capabilities) is installed on the client and throws `SOLANA_ERROR__REACT__MISSING_CAPABILITY` — surfacing the calling `hookName` and a `providerHint` — when it isn't. Plugin-hook authors use this to fail loudly at mount instead of letting a missing plugin surface later as `undefined`. | ||
|
|
||
| Two new error codes (`SOLANA_ERROR__REACT__MISSING_PROVIDER`, `SOLANA_ERROR__REACT__MISSING_CAPABILITY`) are reserved in the `[9000000-9000999]` range. |
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
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
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,77 @@ | ||
| import type { Client } from '@solana/plugin-core'; | ||
| import React from 'react'; | ||
|
|
||
| import { usePromise } from './usePromise'; | ||
|
|
||
| const ClientContext = /*#__PURE__*/ React.createContext<Client<object> | null>(null); | ||
|
|
||
| /** | ||
| * The React context that holds the Kit client published by the nearest {@link ClientProvider}. | ||
| * Exported for advanced cases such as third-party providers that wrap and extend the client; most | ||
| * consumers should reach for {@link useClient} or one of the higher-level hooks instead. | ||
| */ | ||
| export { ClientContext }; | ||
|
|
||
| /** | ||
| * Props accepted by {@link ClientProvider}. | ||
| */ | ||
| export type ClientProviderProps = Readonly<{ | ||
| children?: React.ReactNode; | ||
| /** | ||
| * The Kit client to publish to descendants, or a promise resolving to one (e.g. when the | ||
| * client has async plugins). The reference must be stable across renders — build it at | ||
| * module scope or memoise it with `useMemo` when its config is reactive. | ||
| */ | ||
| client: Client<object> | Promise<Client<object>>; | ||
| }>; | ||
|
|
||
| /** | ||
| * Publishes a caller-owned Kit client to its subtree. Required for `useClient`, | ||
| * `useClientCapability`, and any plugin-specific hook that depends on a client capability. | ||
| * | ||
| * Plugin composition belongs in plain Kit — the provider does no composition, lifecycle | ||
| * management, or disposal; it is a value channel, not a lifecycle channel. When config changes at | ||
| * runtime (e.g. cluster toggle), rebuild the client in `useMemo` and pass the new reference; the | ||
| * subtree resubscribes against the new client identity. | ||
| * | ||
| * Async client support: when `client` is a promise (e.g. `createClient().use(asyncPlugin())`), | ||
| * the provider suspends the subtree via the nearest `<Suspense>` boundary until the promise | ||
| * resolves. On React 19 this delegates to `React.use(promise)`; on React 18 a thrown-promise shim | ||
| * keyed by promise identity preserves the same contract. | ||
| * | ||
| * @example Sync client | ||
| * ```tsx | ||
| * import { createClient } from '@solana/kit'; | ||
| * import { ClientProvider } from '@solana/react'; | ||
| * | ||
| * const client = createClient(); // .use(...) plugins as needed | ||
| * | ||
| * function App() { | ||
| * return ( | ||
| * <ClientProvider client={client}> | ||
| * <MyApp /> | ||
| * </ClientProvider> | ||
| * ); | ||
| * } | ||
| * ``` | ||
| * | ||
| * @example Async client (Suspense) | ||
| * ```tsx | ||
| * const clientPromise = useMemo( | ||
| * () => createClient().use(someAsyncPlugin()), | ||
| * [], | ||
| * ); | ||
| * | ||
| * <Suspense fallback={<Splash />}> | ||
| * <ClientProvider client={clientPromise}> | ||
| * <Shell /> | ||
| * </ClientProvider> | ||
| * </Suspense> | ||
| * ``` | ||
| * | ||
| * @see {@link useClient} | ||
| */ | ||
| export function ClientProvider({ children, client }: ClientProviderProps): React.ReactElement { | ||
| const resolved = usePromise(client); | ||
| return <ClientContext.Provider value={resolved}>{children}</ClientContext.Provider>; | ||
| } | ||
|
mcintyre94 marked this conversation as resolved.
|
||
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,41 @@ | ||
| import React, { ComponentType, ReactElement, ReactNode, StrictMode } from 'react'; | ||
| import { | ||
| render as baseRender, | ||
| renderHook as baseRenderHook, | ||
| RenderHookOptions, | ||
| RenderOptions, | ||
| } from '@testing-library/react'; | ||
|
|
||
| /** | ||
| * Shared test renderers that wrap every React tree in `<StrictMode>`. | ||
| * | ||
| * StrictMode's dev double-render surfaces render-phase impurity (side effects in `useMemo` or | ||
| * `useState` initializers, missing effect cleanups, refs read during render) that would | ||
| * otherwise only manifest in real apps. Using these helpers across all React hook / component | ||
| * tests catches that class of bug at test time. | ||
| * | ||
| * Composes with caller-supplied wrappers: `renderHook(() => useFoo(), { wrapper: Provider })` | ||
| * still works — the `Provider` is rendered inside `StrictMode`. | ||
| * | ||
| * Re-export from this module rather than `@testing-library/react` directly so the StrictMode | ||
| * wrap is automatic. | ||
| */ | ||
|
|
||
| function composeWithStrictMode( | ||
| Inner: ComponentType<{ children: ReactNode }> | undefined, | ||
| ): ComponentType<{ children: ReactNode }> { | ||
| return function StrictModeWrapper({ children }) { | ||
| return <StrictMode>{Inner ? <Inner>{children}</Inner> : children}</StrictMode>; | ||
| }; | ||
| } | ||
|
|
||
| export function renderHook<TResult, TProps>( | ||
| callback: (props: TProps) => TResult, | ||
| options?: RenderHookOptions<TProps>, | ||
| ): ReturnType<typeof baseRenderHook<TResult, TProps>> { | ||
| return baseRenderHook(callback, { ...options, wrapper: composeWithStrictMode(options?.wrapper) }); | ||
| } | ||
|
|
||
| export function render(ui: ReactElement, options?: RenderOptions): ReturnType<typeof baseRender> { | ||
| return baseRender(ui, { ...options, wrapper: composeWithStrictMode(options?.wrapper) }); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.