question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript: Types for useActions hook

See original GitHub issue

Hey!

We want to switch from easy-peasy to redux in order to reduce bundle size (amongst other things). We are working on the refactor/redux branch and it is looking all good but we have some issues with the correct types here:

Can someone help us with this:

export function useActions(actionSelector: (value: any) => any): any {
  const dispatch: ReactFlowDispatch = reduxUseDispatch();

  const action = useMemo(() => {
    // the actions variable is an object with all actions 
    const currAction: any = actionSelector(actions);
    return bindActionCreators(currAction, dispatch);
  }, [dispatch, actionSelector]);

  return action;
}

You can find the code here: https://github.com/wbkd/react-flow/blob/refactor/redux/src/store/hooks.ts#L17-L26

We want to use the hook like this but we are not sure which types we should use for the actionSelector function and the return type of the hook:

// at this point TS should know which params we can pass to the "someAction" action
const someAction = useActions(actions => actions.someAction);

Thanks for your help.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
moklickcommented, Feb 11, 2021

Thanks a lot @alexkuz 😃 This looks pretty good. I think it was the last missing piece in the refactor/redux branch. Without immer we are hopefully more flexible to do some performance improvements inside the redux actions/ reducers. The version is also about 10kb smaller.

0reactions
alexkuzcommented, Feb 11, 2021

So anyway, the TS definition could look like this:

type ActionCreatorSelector<Action> = (acts: typeof actions) => ActionCreator<Action>;
type ActionMapObjectSelector<Action> = (acts: typeof actions) => ActionCreatorsMapObject<Action>;
type ActionSelector<Action> = (acts: typeof actions) =>
  | ActionCreatorsMapObject<Action>
  | ActionCreator<Action>;

// these overrides are in case you want to support both single action and map object of actions
// if not, you can get rid of these and just use ActionCreatorSelector type
export function useActions<Action extends ReactFlowAction>(actionSelector: ActionCreatorSelector<Action>): ActionCreator<Action>;
export function useActions<Action extends ReactFlowAction>(actionSelector: ActionMapObjectSelector<Action>): ActionCreatorsMapObject<Action>;

export function useActions<Action extends ReactFlowAction>(actionSelector: ActionSelector<Action>) {
  const dispatch: ReactFlowDispatch = reduxUseDispatch();

  // the memoization here makes sense only for ActionSelector actually, not so much for ActionMapObjectSelector
  const currAction = actionSelector(actions);

  const action = useMemo(() => {
    // this looks weird but required if both ActionSelector and ActionMapObjectSelector are supported
    return typeof currAction === 'function'
      ? bindActionCreators(currAction, dispatch)
      : bindActionCreators(currAction, dispatch);
  }, [dispatch, currAction]);

  return action;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Hooks - React TypeScript Cheatsheets
However, many hooks are initialized with null-ish default values, and you may wonder how to provide types. Explicitly declare the type, and use...
Read more >
Usage With TypeScript - Redux
Since these are actual variables, not types, it's important to define them in a separate file such as app/hooks.ts , not the store...
Read more >
React & TypeScript: how to type hooks (a complete guide)
Hooks are a fundamental aspect of writing React components, so knowing how to type them is vital to using TypeScript with React.
Read more >
react-redux-actions-hook - npm
Factory to create useActions hooks for use with react-redux. ... TypeScript icon, indicating that this package has built-in type ...
Read more >
useActions hook - CodeSandbox
Activating extension 'vscode.typescript-language-features' failed: Could not find bundled tsserver.js. DashboardCtrl+Escape ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found