Blog>
Snippets

Updating State with Actions in TanStack Store

Provide an example of how to define and dispatch actions to update the state within the TanStack Store, using a simple counter app scenario.
import { createStore, action } from 'easy-peasy';
Import createStore and action from easy-peasy to create the store and define actions.
const storeModel = {
  count: 0, // Initial state of the count
  increment: action((state) => {
    state.count += 1;
  }), // Action to increment the count
  decrement: action((state) => {
    state.count -= 1;
  }) // Action to decrement the count
};
Define the store model with initial state and actions for incrementing and decrementing the count.
const store = createStore(storeModel);
Create the store using the model defined above.
store.dispatch.storeModel.increment();
Dispatch the increment action to update the state by increasing the count.
store.dispatch.storeModel.decrement();
Dispatch the decrement action to update the state by decreasing the count.