Skip to content

Instantly share code, notes, and snippets.

@fostyfost
Created December 7, 2021 08:35
Show Gist options
  • Save fostyfost/edacf9d0b5f29078acd23fadf42dee33 to your computer and use it in GitHub Desktop.
Save fostyfost/edacf9d0b5f29078acd23fadf42dee33 to your computer and use it in GitHub Desktop.
`usePropsSelector` from `redux-views`
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