-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
@wordpress/data: Introduce useSelect custom hook. (#15737)
A new `useSelect` hook for the `wp.data` api! This hook allows for an alternative way to interact with data kept in a store state via registered selectors following a similar pattern to `useEffect` and other react hooks. Other notable changes in this pull: - exposing the custom `useRegistry` react hook for retrieving the `registry` object from components in a registry provider tree. - `withSelect` implements `useSelect` under the hood. Many thanks also to @epiqueras for contributions to the work in this pull.
- Loading branch information
Showing
15 changed files
with
875 additions
and
338 deletions.
There are no files selected for viewing
This file contains 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 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
12 changes: 12 additions & 0 deletions
12
packages/data/src/components/async-mode-provider/context.js
This file contains 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,12 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createContext } from '@wordpress/element'; | ||
|
||
export const Context = createContext( false ); | ||
|
||
const { Consumer, Provider } = Context; | ||
|
||
export const AsyncModeConsumer = Consumer; | ||
|
||
export default Provider; |
This file contains 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 |
---|---|---|
@@ -1,10 +1,2 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createContext } from '@wordpress/element'; | ||
|
||
const { Consumer, Provider } = createContext( false ); | ||
|
||
export const AsyncModeConsumer = Consumer; | ||
|
||
export default Provider; | ||
export { default as useAsyncMode } from './use-async-mode'; | ||
export { default as AsyncModeProvider, AsyncModeConsumer } from './context'; |
13 changes: 13 additions & 0 deletions
13
packages/data/src/components/async-mode-provider/use-async-mode.js
This file contains 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,13 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useContext } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { Context } from './context'; | ||
|
||
export default function useAsyncMode() { | ||
return useContext( Context ); | ||
} |
This file contains 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,54 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createContext } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import defaultRegistry from '../../default-registry'; | ||
|
||
export const Context = createContext( defaultRegistry ); | ||
|
||
const { Consumer, Provider } = Context; | ||
|
||
/** | ||
* A custom react Context consumer exposing the provided `registry` to | ||
* children components. Used along with the RegistryProvider. | ||
* | ||
* You can read more about the react context api here: | ||
* https://reactjs.org/docs/context.html#contextprovider | ||
* | ||
* @example | ||
* ```js | ||
* const { | ||
* RegistryProvider, | ||
* RegistryConsumer, | ||
* createRegistry | ||
* } = wp.data; | ||
* | ||
* const registry = createRegistry( {} ); | ||
* | ||
* const App = ( { props } ) => { | ||
* return <RegistryProvider value={ registry }> | ||
* <div>Hello There</div> | ||
* <RegistryConsumer> | ||
* { ( registry ) => ( | ||
* <ComponentUsingRegistry | ||
* { ...props } | ||
* registry={ registry } | ||
* ) } | ||
* </RegistryConsumer> | ||
* </RegistryProvider> | ||
* } | ||
*/ | ||
export const RegistryConsumer = Consumer; | ||
|
||
/** | ||
* A custom Context provider for exposing the provided `registry` to children | ||
* components via a consumer. | ||
* | ||
* See <a name="#RegistryConsumer">RegistryConsumer</a> documentation for | ||
* example. | ||
*/ | ||
export default Provider; |
This file contains 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 |
---|---|---|
@@ -1,15 +1,2 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createContext } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import defaultRegistry from '../../default-registry'; | ||
|
||
const { Consumer, Provider } = createContext( defaultRegistry ); | ||
|
||
export const RegistryConsumer = Consumer; | ||
|
||
export default Provider; | ||
export { default as RegistryProvider, RegistryConsumer } from './context'; | ||
export { default as useRegistry } from './use-registry'; |
52 changes: 52 additions & 0 deletions
52
packages/data/src/components/registry-provider/use-registry.js
This file contains 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,52 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useContext } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { Context } from './context'; | ||
|
||
/** | ||
* A custom react hook exposing the registry context for use. | ||
* | ||
* This exposes the `registry` value provided via the | ||
* <a href="#RegistryProvider">Registry Provider</a> to a component implementing | ||
* this hook. | ||
* | ||
* It acts similarly to the `useContext` react hook. | ||
* | ||
* Note: Generally speaking, `useRegistry` is a low level hook that in most cases | ||
* won't be needed for implementation. Most interactions with the wp.data api | ||
* can be performed via the `useSelect` hook, or the `withSelect` and | ||
* `withDispatch` higher order components. | ||
* | ||
* @example | ||
* ```js | ||
* const { | ||
* RegistryProvider, | ||
* createRegistry, | ||
* useRegistry, | ||
* } = wp.data | ||
* | ||
* const registry = createRegistry( {} ); | ||
* | ||
* const SomeChildUsingRegistry = ( props ) => { | ||
* const registry = useRegistry( registry ); | ||
* // ...logic implementing the registry in other react hooks. | ||
* }; | ||
* | ||
* | ||
* const ParentProvidingRegistry = ( props ) => { | ||
* return <RegistryProvider value={ registry }> | ||
* <SomeChildUsingRegistry { ...props } /> | ||
* </RegistryProvider> | ||
* }; | ||
* ``` | ||
* | ||
* @return {Function} A custom react hook exposing the registry context value. | ||
*/ | ||
export default function useRegistry() { | ||
return useContext( Context ); | ||
} |
Oops, something went wrong.