Actions

    Actions are payloads of information that send data from your application to your store. They are the only source of information for the store. You send them to the store using .

    Here's an example action which represents adding a new todo item:

    1. {
    2. type: ADD_TODO,
    3. text: 'Build my first Redux app'
    4. }

    Actions are plain JavaScript objects. Actions must have a type property that indicates the type of action being performed. Types should typically be defined as string constants. Once your app is large enough, you may want to move them into a separate module.

    1. import { ADD_TODO, REMOVE_TODO } from '../actionTypes'

    You don't have to define action type constants in a separate file, or even to define them at all. For a small project, it might be easier to just use string literals for action types. However, there are some benefits to explicitly declaring constants in larger codebases. Read for more practical tips on keeping your codebase clean.

    Other than type, the structure of an action object is really up to you. If you're interested, check out Flux Standard Action for recommendations on how actions could be constructed.

    It's a good idea to pass as little data in each action as possible. For example, it's better to pass index than the whole todo object.

    Finally, we'll add one more action type for changing the currently visible todos.

    1. {
    2. type: SET_VISIBILITY_FILTER,
    3. filter: SHOW_COMPLETED
    4. }

    Action creators are exactly that—functions that create actions. It's easy to conflate the terms “action” and “action creator”, so do your best to use the proper term.

    In Redux, action creators simply return an action:

    1. function addTodo(text) {
    2. return {
    3. type: ADD_TODO,
    4. text
    5. }

    This makes them portable and easy to test.

    In Redux this is not the case.Instead, to actually initiate a dispatch, pass the result to the dispatch() function:

    1. dispatch(completeTodo(index))

    Alternatively, you can create a bound action creator that automatically dispatches:

    1. const boundAddTodo = text => dispatch(addTodo(text))
    2. const boundCompleteTodo = index => dispatch(completeTodo(index))

    Now you'll be able to call them directly:

    The dispatch() function can be accessed directly from the store as , but more likely you'll access it using a helper like react-redux's connect(). You can use to automatically bind many action creators to a dispatch() function.

    Action creators can also be asynchronous and have side-effects. You can read about async actions in the to learn how to handle AJAX responses and compose action creators into async control flow. Don't skip ahead to async actions until you've completed the basics tutorial, as it covers other important concepts that are prerequisite for the advanced tutorial and async actions.

    1. /*
    2. * action types
    3. */
    4. export const ADD_TODO = 'ADD_TODO'
    5. export const TOGGLE_TODO = 'TOGGLE_TODO'
    6. export const SET_VISIBILITY_FILTER = 'SET_VISIBILITY_FILTER'
    7. /*
    8. * other constants
    9. */
    10. SHOW_ALL: 'SHOW_ALL',
    11. SHOW_ACTIVE: 'SHOW_ACTIVE'
    12. }
    13. /*
    14. * action creators
    15. */
    16. export function addTodo(text) {
    17. return { type: ADD_TODO, text }
    18. }
    19. export function toggleTodo(index) {
    20. return { type: TOGGLE_TODO, index }
    21. }
    22. export function setVisibilityFilter(filter) {
    23. return { type: SET_VISIBILITY_FILTER, filter }
    24. }