Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

usePropsSelector with Typescript #29

Open
martinkutter opened this issue May 18, 2021 · 1 comment
Open

usePropsSelector with Typescript #29

martinkutter opened this issue May 18, 2021 · 1 comment

Comments

@martinkutter
Copy link

Hey, thank you for this great library. 馃憤馃徎
Can you help how a Typescript variant of the usePropsSelector would look like?

const usePropsSelector = (selector, props) => {
  const id = selector.idSelector && selector.idSelector(null, props);
  useEffect(() => selector.use && selector.use(id), [selector, id]);

  return useSelector(x => selector(x, props));
};
@fostyfost
Copy link

@martinkutter Hi!
Please, try this

import * as React from 'react'
import { useSelector } from 'react-redux'
import type { OutputSelector, OutputParametricSelector } from 'redux-views'

type Sel = (OutputSelector<any, any, any> | OutputParametricSelector<any, any, any, any>) & {
  use?: (...args: any[]) => any
  idSelector?: (...args: any[]) => any
}

export const usePropsSelector = <T extends Sel = Sel>(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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants