piotrwitek / typesafe-actions

Typesafe utilities for "action-creators" in Redux / Flux Architecture
https://codesandbox.io/s/github/piotrwitek/typesafe-actions/tree/master/codesandbox
MIT License
2.41k stars 98 forks source link

How do you write an action creator with a default parameter? #50

Closed sylvanaar closed 6 years ago

sylvanaar commented 6 years ago

Had you considered parameter defaults in your design?

Right now I am doing:

(payload: number = 1) => createStandardAction(CHANGE_VIEW)<number>()(payload)

But it seems like it could be improved upon so you don't have to repeat the payload type.

Thanks for the best redux action typings around!

piotrwitek commented 6 years ago

Hey! For your case you can use action factory, which will provide equivalent result:

import { action } from 'typesafe-actions'
const changeView = (payload: number = 1) => action(CHANGE_VIEW, payload)

Is there any reason why you didn't use that?

EDIT: I'm proposing this solution first because I don't see you use the benefits from createStandardAction factory, but if you really want to use it because reasons ;) you can always try to use map method like here: https://github.com/piotrwitek/typesafe-actions-todo-app/blob/master/src/features/todos/actions.ts#L11 Is it good enough or you have some suggestions to improve?

PS: I'm glad you like the library!

sylvanaar commented 6 years ago

Thanks. I think I should have read the documentation a little more! The design is way more flexible that I thought.

Thanks again!