Created
December 7, 2021 08:35
-
-
Save fostyfost/edacf9d0b5f29078acd23fadf42dee33 to your computer and use it in GitHub Desktop.
`usePropsSelector` from `redux-views`
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
import * as React from 'react' | |
import { useSelector } from 'react-redux' | |
import type { OutputSelector, OutputParametricSelector } from 'redux-views' | |
type SelectorWithIdAndUse = (OutputSelector<any, any, any> | OutputParametricSelector<any, any, any, any>) & { | |
use?: (...args: any[]) => any | |
idSelector?: (...args: any[]) => any | |
} | |
export const usePropsSelector = <T extends SelectorWithIdAndUse = SelectorWithIdAndUse>( | |
selector: T, | |
props: any, | |
): ReturnType<typeof selector> => { | |
const id = selector.idSelector && selector.idSelector(null, props) | |
React.useEffect(() => selector.use && selector.use(id), [selector, id]) | |
return useSelector(state => selector(state, props)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment