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

Is bounded actions example ok? #1391

Closed
mauron85 opened this issue Feb 11, 2016 · 2 comments
Closed

Is bounded actions example ok? #1391

mauron85 opened this issue Feb 11, 2016 · 2 comments

Comments

@mauron85
Copy link

Hi, I've just jumped into Redux and like it very much so far.
Not sure if I understand completely how it works, but in Actions there is section about bounded action creator

Alternatively, you can create a bound action creator that automatically dispatches:
const boundAddTodo = (text) => dispatch(addTodo(text))
const boundCompleteTodo = (index) => dispatch(completeTodo(index))

If I understand correctly that is possible because of redux-thunk which has to be registered as middleware. And if so, shouldn't it be then be:

const boundAddTodo = (text) => (dispatch) => dispatch((addTodo(text)))

Babel try it

@tget4
Copy link

tget4 commented Feb 11, 2016

This has nothing to do with Redux Thunk. The term 'bound action creator' refers to the result of composing dispatch and an action creator. When this function is called it automatically dispatches the action creator. Passing this function to components allows them to be decoupled from dispatcher and actions.

It would look like this:

const boundAddTodo = (text) => dispatch(addTodo(text))
<Todo onClick={boundAddTodo}  />

@gaearon
Copy link
Contributor

gaearon commented Feb 11, 2016

This is an example from early version of docs and isn’t very relevant today.
Maybe we should just remove it as it confuses people.

Yes, the original point was to show that addTodo() by itself doesn’t do anything and you need to call dispatch(addTodo()) to actually cause the action to happen. At which point you can “bind” it to a certain dispatch function so you can just addTodo(). You can do this by hand, with bindActionCreators(), or you can use React Redux which does this for you.

@gaearon gaearon closed this as completed Feb 11, 2016
gaearon added a commit that referenced this issue Feb 12, 2016
This issue was closed.
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

3 participants