useStoreActions

A hook granting your components access to the store's actions.

const addTodo = useStoreActions(actions => actions.todos.add);

Arguments

  • mapActions (Function, required)

    The function that is used to resolve the action that your component requires. It receives the following arguments:

    • actions (Object)

      The actions of your store.

Example

import { useState } from 'react';
import { useStoreActions } from 'easy-peasy';

const AddTodo = () => {
  const [text, setText] = useState('');
  const addTodo = useStoreActions(actions => actions.todos.add);
  return (
    <div>
      <input value={text} onChange={(e) => setText(e.target.value)} />
      <button onClick={() => addTodo(text)}>Add</button>
    </div>
  );
};