Typescript: Types for useActions hook
See original GitHub issueHey!
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:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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.
So anyway, the TS definition could look like this: