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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MapDispatchToProps shorthand #205

Closed
Vanuan opened this issue Feb 11, 2020 · 5 comments
Closed

MapDispatchToProps shorthand #205

Vanuan opened this issue Feb 11, 2020 · 5 comments

Comments

@Vanuan
Copy link

Vanuan commented Feb 11, 2020

Is there a solution for mapDispatchToProps shorthand syntax?

interface IProps {
  action: () => void;
}

const MyComponent = ({ action }: IProps): JSX.Element => (
  <div onClick={action}>my component</div>
);

const action = actionCreator<null>('SOME_ACTION'); // bound to dispatch

connect(null, { action })(MyComponent);

I'm getting this error:

Type 'ActionCreator<void>' is not assignable to type '() => void'.  TS2769

Is there a way to fix it without changing prop types of MyComponent?

@piotrwitek
Copy link
Owner

Hey @Vanuan
Yes, an example is here: https://github.com/piotrwitek/react-redux-typescript-guide#--redux-connected-counter-with-redux-thunk-integration

use this:

const mapDispatchToProps = (dispatch: Dispatch<Types.RootAction>) =>
  bindActionCreators(
    {
      onIncrement: incrementWithDelay,
    },
    dispatch
  );

@Vanuan
Copy link
Author

Vanuan commented Feb 26, 2020

But that's not a shorthand syntax

@Vanuan
Copy link
Author

Vanuan commented Feb 26, 2020

Shorthand syntax is this:

connect(null, { onIncrement: incrementWithDelay })

@Vanuan
Copy link
Author

Vanuan commented Feb 26, 2020

Is there any reason you want to be able to use a return value of the dispatch call?

e.g.: props.incrementWithDelay().then(doSomethingElse)

This looks like a smell. But I think return value can be safely ignored.

So () => Action<void> should be assignable to () => void

@Vanuan
Copy link
Author

Vanuan commented Feb 26, 2020

I've created a fix for typescript-fsa:
aikoven/typescript-fsa#82

Maybe you can use the same approach

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