Blog>
Snippets

Creating Redux Action Creators with Arrow Functions

Use arrow functions to tidy up and streamline the creation of action creators in Redux.
// Action Types
const INCREMENT = 'INCREMENT';
const DECREMENT = 'DECREMENT';
const SET = 'SET';

// Action Creators using Arrow Functions

// Increment action creator
export const increment = () => ({
  type: INCREMENT
});

// Decrement action creator
export const decrement = () => ({
  type: DECREMENT
});

// Set action creator with payload
export const set = (value) => ({
  type: SET,
  payload: value
});
This code defines three action types and their corresponding action creators using arrow functions. The 'increment' and 'decrement' action creators simply return actions with their respective types, while the 'set' action creator returns an action with a 'payload' containing a value to be used in the reducer function.