Convert Figma logo to code with AI

ctrlplusb logoeasy-peasy

Vegetarian friendly state for React

5,029
189
5,029
13

Top Related Projects

8,477

The Redux Framework

The official, opinionated, batteries-included toolset for efficient Redux development

46,119

🐻 Bear necessities for state management in React

27,458

Simple, scalable state management.

Business logic with ease ☄️

Quick Overview

Easy Peasy is a lightweight state management library for React applications. It provides a simple and intuitive API for managing global state, built on top of Redux and Redux Toolkit. Easy Peasy aims to reduce boilerplate code and make state management more accessible for React developers.

Pros

  • Simple and intuitive API, reducing the learning curve for state management
  • Built-in support for async actions and derived state (computed values)
  • Automatic handling of immutability and optimized re-renders
  • TypeScript support out of the box

Cons

  • Limited ecosystem compared to pure Redux
  • May not be suitable for very large-scale applications with complex state requirements
  • Potential performance overhead for extremely simple applications
  • Less flexibility compared to using Redux directly

Code Examples

  1. Creating a store:
import { createStore } from 'easy-peasy';

const store = createStore({
  todos: {
    items: [],
    addTodo: (state, payload) => {
      state.items.push(payload);
    },
  },
});
  1. Using the store in a component:
import { useStoreActions, useStoreState } from 'easy-peasy';

const TodoList = () => {
  const todos = useStoreState((state) => state.todos.items);
  const addTodo = useStoreActions((actions) => actions.todos.addTodo);

  return (
    <div>
      {todos.map((todo) => (
        <div key={todo.id}>{todo.text}</div>
      ))}
      <button onClick={() => addTodo({ id: Date.now(), text: 'New Todo' })}>
        Add Todo
      </button>
    </div>
  );
};
  1. Using computed values:
import { computed } from 'easy-peasy';

const store = createStore({
  todos: {
    items: [],
    completedTodos: computed((state) => state.items.filter((todo) => todo.completed)),
  },
});

Getting Started

  1. Install Easy Peasy:
npm install easy-peasy
  1. Create a store:
import { createStore } from 'easy-peasy';

const store = createStore({
  // Your model here
});
  1. Wrap your app with the StoreProvider:
import { StoreProvider } from 'easy-peasy';

const App = () => (
  <StoreProvider store={store}>
    {/* Your app components */}
  </StoreProvider>
);
  1. Use the store in your components:
import { useStoreState, useStoreActions } from 'easy-peasy';

const MyComponent = () => {
  const someState = useStoreState((state) => state.someState);
  const someAction = useStoreActions((actions) => actions.someAction);
  // Use state and actions in your component
};

Competitor Comparisons

8,477

The Redux Framework

Pros of Rematch

  • More flexible and customizable, allowing for advanced use cases and complex state management
  • Better TypeScript support out of the box, with stronger type inference
  • Supports plugins for extending functionality, such as loading and persistence

Cons of Rematch

  • Steeper learning curve, especially for developers new to Redux
  • More boilerplate code required for setup and configuration
  • Less opinionated, which can lead to inconsistencies across projects

Code Comparison

Easy-peasy:

const store = createStore({
  todos: {
    items: [],
    addTodo: action((state, payload) => {
      state.items.push(payload)
    })
  }
})

Rematch:

const todos = {
  state: { items: [] },
  reducers: {
    addTodo(state, payload) {
      return { ...state, items: [...state.items, payload] }
    }
  }
}

const store = init({
  models: { todos }
})

Both Easy-peasy and Rematch aim to simplify Redux state management, but they take different approaches. Easy-peasy focuses on simplicity and ease of use, while Rematch offers more flexibility and customization options. Easy-peasy uses immer for immutable updates, allowing for direct state mutations, while Rematch follows a more traditional Redux pattern with explicit state updates.

The official, opinionated, batteries-included toolset for efficient Redux development

Pros of Redux Toolkit

  • Official Redux package, ensuring long-term support and compatibility
  • Includes utilities for common Redux use cases, reducing boilerplate code
  • Integrates well with existing Redux ecosystems and tools

Cons of Redux Toolkit

  • Steeper learning curve for beginners compared to Easy Peasy
  • Requires more setup and configuration for basic functionality
  • Can be more verbose for simple state management scenarios

Code Comparison

Redux Toolkit:

import { createSlice } from '@reduxjs/toolkit'

const counterSlice = createSlice({
  name: 'counter',
  initialState: 0,
  reducers: {
    increment: state => state + 1,
  },
})

Easy Peasy:

import { action, createStore } from 'easy-peasy'

const store = createStore({
  count: 0,
  increment: action((state) => {
    state.count += 1
  }),
})

Both Redux Toolkit and Easy Peasy aim to simplify Redux state management, but they take different approaches. Redux Toolkit provides a more comprehensive set of tools and follows Redux conventions more closely, while Easy Peasy offers a more streamlined and opinionated approach that may be easier for beginners to grasp. The choice between the two depends on project requirements, team expertise, and personal preferences.

46,119

🐻 Bear necessities for state management in React

Pros of zustand

  • Lightweight and minimalistic, with a smaller bundle size
  • More flexible and unopinionated, allowing for custom middleware and state management patterns
  • Supports TypeScript out of the box with excellent type inference

Cons of zustand

  • Less opinionated structure, which may lead to inconsistent code organization in larger projects
  • Fewer built-in features compared to Easy Peasy, requiring more manual implementation for advanced use cases

Code Comparison

zustand:

import create from 'zustand'

const useStore = create((set) => ({
  count: 0,
  increment: () => set((state) => ({ count: state.count + 1 })),
}))

Easy Peasy:

import { createStore, action } from 'easy-peasy'

const store = createStore({
  count: 0,
  increment: action((state) => { state.count += 1 }),
})

Both zustand and Easy Peasy offer simple and intuitive APIs for state management in React applications. zustand provides a more lightweight and flexible approach, while Easy Peasy offers a more structured and opinionated solution with additional built-in features. The choice between the two depends on project requirements, team preferences, and the desired level of abstraction in state management.

27,458

Simple, scalable state management.

Pros of MobX

  • More flexible and unopinionated, allowing for diverse state management approaches
  • Better performance for complex applications with large state trees
  • Extensive ecosystem with additional libraries and tools

Cons of MobX

  • Steeper learning curve, especially for developers new to reactive programming
  • Less predictable state updates due to mutable state
  • Requires more boilerplate code for optimal TypeScript support

Code Comparison

MobX:

import { makeAutoObservable } from "mobx";

class TodoStore {
  todos = [];
  constructor() {
    makeAutoObservable(this);
  }
  addTodo(text) {
    this.todos.push({ text, completed: false });
  }
}

Easy Peasy:

import { action, createStore } from "easy-peasy";

const todoModel = {
  todos: [],
  addTodo: action((state, payload) => {
    state.todos.push({ text: payload, completed: false });
  }),
};

const store = createStore(todoModel);

Both MobX and Easy Peasy offer state management solutions for React applications, but they differ in their approaches. MobX provides a more flexible and powerful system, suitable for complex applications, while Easy Peasy offers a simpler, more opinionated approach that may be easier for beginners to grasp. The choice between them depends on the specific needs of your project and your team's expertise.

Business logic with ease ☄️

Pros of Effector

  • More flexible and powerful, allowing for complex state management scenarios
  • Better TypeScript support with stricter typing
  • Larger ecosystem with additional tools and libraries

Cons of Effector

  • Steeper learning curve due to its more complex API
  • Less opinionated, which may lead to inconsistent usage patterns across projects
  • Requires more boilerplate code for simple use cases

Code Comparison

Easy-peasy:

import { action, createStore } from 'easy-peasy';

const store = createStore({
  todos: [],
  addTodo: action((state, payload) => {
    state.todos.push(payload);
  }),
});

Effector:

import { createStore, createEvent } from 'effector';

const addTodo = createEvent();
const $todos = createStore([])
  .on(addTodo, (state, payload) => [...state, payload]);

Both libraries aim to simplify state management in React applications, but they take different approaches. Easy-peasy focuses on simplicity and ease of use, making it ideal for smaller projects or developers new to state management. Effector, on the other hand, offers more power and flexibility, making it suitable for larger, more complex applications. The choice between the two depends on the specific needs of your project and your team's expertise.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

 

Vegetarian friendly state for React

 

npm MIT License Codecov

Easy Peasy is an abstraction of Redux, providing a reimagined API that focuses on developer experience. It allows you to quickly and easily manage your state, whilst leveraging the strong architectural guarantees and extensive eco-system that Redux has to offer.

  • Zero configuration
  • No boilerplate
  • React hooks based API
  • Extensive TypeScript support
  • Encapsulate data fetching
  • Computed properties
  • Reactive actions
  • Redux middleware support
  • State persistence
  • Redux Dev Tools
  • Global, context, or local stores
  • Built-in testing utils
  • React Native supported
  • Hot reloading supported

 

All of this comes via a single dependency install.

npm install easy-peasy

 

Fly like an eagle 🦅

Create your store

const store = createStore({
  todos: ['Create store', 'Wrap application', 'Use store'],

  addTodo: action((state, payload) => {
    state.todos.push(payload);
  }),
});

Wrap your application

function App() {
  return (
    <StoreProvider store={store}>
      <TodoList />
    </StoreProvider>
  );
}

Use the store

function TodoList() {
  const todos = useStoreState((state) => state.todos);
  const addTodo = useStoreActions((actions) => actions.addTodo);
  return (
    <div>
      {todos.map((todo, idx) => (
        <div key={idx}>{todo}</div>
      ))}
      <AddTodo onAdd={addTodo} />
    </div>
  );
}

 

Examples 📚

See the example folder for more examples of how to use easy-peasy.

 

Core Team 🛠


Peter Weinberg

Jørn A. Myrland

Sean Matheson

 

Our Sponsors ❤️

We have only but great appreciation to those who support this project. If you have the ability to help contribute towards the continued maintenance and evolution of this library then please consider [becoming a sponsor].

 

Documentation

See the official website for tutorials, docs, recipes, and more.

 

OS Awards Nominee

Easy Peasy was nominated under the "Productivity Booster" category.

NPM DownloadsLast 30 Days