-
Notifications
You must be signed in to change notification settings - Fork 4k
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
@wordpress/data: Introduce new custom useDispatch
react hook
#15896
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
d54e692
initial commit with two new dispatch hooks.
nerrad 0db22f7
replace non-function property values with a function
nerrad 2c15377
try alternate approach
nerrad 2f1e32d
try with useMemo
nerrad 6f3ee01
conditionally return either dispatch or specific store actions
nerrad 9c9df2f
don’t use proxy and move initial dispatchMap call into useMemo callback
nerrad 9dcce6f
remove unnecessary use of useMemo
nerrad 690830c
make sure latest dispatchMap is invoked
nerrad 5ee59b6
shorten syntax
nerrad 89a7fb4
replace old withDispatch with new
nerrad 70845bb
add documentation and export useDispatch
nerrad 33293b2
add documentation and implement useLayoutEffect
nerrad 50e8a70
update to README
nerrad 6946a6a
implement deps on useDispatchWithMap
nerrad 6500117
remove unnecessary pure call and improve docs
nerrad 4b19d7c
update with-dispatch tests
nerrad 5137db6
fix jsdoc for optional param
nerrad 16b2b04
add tests for useDispatch hook
nerrad 0426938
add changelog entry
nerrad 540860b
fix spacing in documentation
nerrad 21189e7
more spacing fixes
nerrad 326fba1
more documentation spacing fixes
nerrad File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,2 @@ | ||
export { default as useDispatch } from './use-dispatch'; | ||
export { default as useDispatchWithMap } from './use-dispatch-with-map'; |
137 changes: 137 additions & 0 deletions
137
packages/data/src/components/use-dispatch/test/use-dispatch.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,137 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import TestRenderer, { act } from 'react-test-renderer'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useDispatch from '../use-dispatch'; | ||
import { createRegistry } from '../../../registry'; | ||
import { RegistryProvider } from '../../registry-provider'; | ||
|
||
describe( 'useDispatch', () => { | ||
let registry; | ||
beforeEach( () => { | ||
registry = createRegistry(); | ||
} ); | ||
|
||
it( 'returns dispatch function from store with no store name provided', () => { | ||
registry.registerStore( 'demoStore', { | ||
reducer: ( state ) => state, | ||
actions: { | ||
foo: () => 'bar', | ||
}, | ||
} ); | ||
const TestComponent = () => { | ||
return <div></div>; | ||
}; | ||
const Component = () => { | ||
const dispatch = useDispatch(); | ||
return <TestComponent dispatch={ dispatch } />; | ||
}; | ||
|
||
let testRenderer; | ||
act( () => { | ||
testRenderer = TestRenderer.create( | ||
<RegistryProvider value={ registry }> | ||
<Component /> | ||
</RegistryProvider> | ||
); | ||
} ); | ||
|
||
const testInstance = testRenderer.root; | ||
|
||
expect( testInstance.findByType( TestComponent ).props.dispatch ) | ||
.toBe( registry.dispatch ); | ||
} ); | ||
it( 'returns expected action creators from store for given storeName', () => { | ||
const noop = () => ( { type: '__INERT__' } ); | ||
const testAction = jest.fn().mockImplementation( noop ); | ||
registry.registerStore( 'demoStore', { | ||
reducer: ( state ) => state, | ||
actions: { | ||
foo: testAction, | ||
}, | ||
} ); | ||
const TestComponent = () => { | ||
const { foo } = useDispatch( 'demoStore' ); | ||
return <button onClick={ foo } />; | ||
}; | ||
|
||
let testRenderer; | ||
|
||
act( () => { | ||
testRenderer = TestRenderer.create( | ||
<RegistryProvider value={ registry } > | ||
<TestComponent /> | ||
</RegistryProvider> | ||
); | ||
} ); | ||
|
||
const testInstance = testRenderer.root; | ||
|
||
act( () => { | ||
testInstance.findByType( 'button' ).props.onClick(); | ||
} ); | ||
|
||
expect( testAction ).toHaveBeenCalledTimes( 1 ); | ||
} ); | ||
it( 'returns dispatch from correct registry if registries change', () => { | ||
const reducer = ( state ) => state; | ||
const noop = () => ( { type: '__INERT__' } ); | ||
const firstRegistryAction = jest.fn().mockImplementation( noop ); | ||
const secondRegistryAction = jest.fn().mockImplementation( noop ); | ||
|
||
const firstRegistry = registry; | ||
firstRegistry.registerStore( 'demo', { | ||
reducer, | ||
actions: { | ||
noop: firstRegistryAction, | ||
}, | ||
} ); | ||
|
||
const TestComponent = () => { | ||
const dispatch = useDispatch(); | ||
return <button onClick={ () => dispatch( 'demo' ).noop() } />; | ||
}; | ||
|
||
let testRenderer; | ||
act( () => { | ||
testRenderer = TestRenderer.create( | ||
<RegistryProvider value={ firstRegistry }> | ||
<TestComponent /> | ||
</RegistryProvider> | ||
); | ||
} ); | ||
const testInstance = testRenderer.root; | ||
|
||
act( () => { | ||
testInstance.findByType( 'button' ).props.onClick(); | ||
} ); | ||
|
||
expect( firstRegistryAction ).toHaveBeenCalledTimes( 1 ); | ||
expect( secondRegistryAction ).toHaveBeenCalledTimes( 0 ); | ||
|
||
const secondRegistry = createRegistry(); | ||
secondRegistry.registerStore( 'demo', { | ||
reducer, | ||
actions: { | ||
noop: secondRegistryAction, | ||
}, | ||
} ); | ||
|
||
act( () => { | ||
testRenderer.update( | ||
<RegistryProvider value={ secondRegistry }> | ||
<TestComponent /> | ||
</RegistryProvider> | ||
); | ||
} ); | ||
act( () => { | ||
testInstance.findByType( 'button' ).props.onClick(); | ||
} ); | ||
expect( firstRegistryAction ).toHaveBeenCalledTimes( 1 ); | ||
expect( secondRegistryAction ).toHaveBeenCalledTimes( 1 ); | ||
} ); | ||
} ); |
71 changes: 71 additions & 0 deletions
71
packages/data/src/components/use-dispatch/use-dispatch-with-map.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,71 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { mapValues } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo, useRef, useEffect, useLayoutEffect } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useRegistry from '../registry-provider/use-registry'; | ||
|
||
/** | ||
* Favor useLayoutEffect to ensure the store subscription callback always has | ||
* the dispatchMap from the latest render. If a store update happens between | ||
* render and the effect, this could cause missed/stale updates or | ||
* inconsistent state. | ||
* | ||
* Fallback to useEffect for server rendered components because currently React | ||
* throws a warning when using useLayoutEffect in that environment. | ||
*/ | ||
const useIsomorphicLayoutEffect = | ||
typeof window !== 'undefined' ? useLayoutEffect : useEffect; | ||
|
||
/** | ||
* Custom react hook for returning aggregate dispatch actions using the provided | ||
* dispatchMap. | ||
* | ||
* Currently this is an internal api only and is implemented by `withDispatch` | ||
* | ||
* @param {Function} dispatchMap Receives the `registry.dispatch` function as | ||
* the first argument and the `registry` object | ||
* as the second argument. Should return an | ||
* object mapping props to functions. | ||
* @param {Array} deps An array of dependencies for the hook. | ||
* @return {Object} An object mapping props to functions created by the passed | ||
* in dispatchMap. | ||
*/ | ||
const useDispatchWithMap = ( dispatchMap, deps ) => { | ||
const registry = useRegistry(); | ||
const currentDispatchMap = useRef( dispatchMap ); | ||
|
||
useIsomorphicLayoutEffect( () => { | ||
currentDispatchMap.current = dispatchMap; | ||
} ); | ||
|
||
return useMemo( () => { | ||
const currentDispatchProps = currentDispatchMap.current( | ||
registry.dispatch, | ||
registry | ||
); | ||
return mapValues( | ||
currentDispatchProps, | ||
( dispatcher, propName ) => { | ||
if ( typeof dispatcher !== 'function' ) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
`Property ${ propName } returned from dispatchMap in useDispatchWithMap must be a function.` | ||
); | ||
} | ||
return ( ...args ) => currentDispatchMap | ||
.current( registry.dispatch, registry )[ propName ]( ...args ); | ||
} | ||
); | ||
}, [ registry, ...deps ] ); | ||
}; | ||
|
||
export default useDispatchWithMap; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What purpose does
deps
serve if this is an internal hook, and the only place we use it, we always pass an empty array?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point. Right now with this being an internal only implementation it's obviously an unneeded variable. There may be potential for this hook being exposed at some point as a public api (if use-case demonstrates need) in which case there is utility for the deps arg.