Convert Figma logo to code with AI

facebookexperimental logoRecoil

Recoil is an experimental state management library for React apps. It provides several capabilities that are difficult to achieve with React alone, while being compatible with the newest features of React.

19,568
1,178
19,568
325

Top Related Projects

Official React bindings for Redux

27,458

Simple, scalable state management.

60,792

A JS library for predictable global state management

46,119

🐻 Bear necessities for state management in React

Quick Overview

Recoil is a state management library for React applications, developed by Facebook. It provides a way to manage global state in React apps with a more flexible and performant approach compared to traditional state management solutions like Redux.

Pros

  • Easier to learn and use compared to more complex state management libraries
  • Supports derived state (selectors) out of the box, allowing for efficient computation of derived data
  • Integrates well with React's concurrent mode and suspense features
  • Allows for fine-grained updates, potentially improving performance in large applications

Cons

  • Relatively new library, which means less community support and fewer resources compared to more established solutions
  • Limited ecosystem of middleware and tools compared to Redux
  • Requires React as a peer dependency, limiting its use to React applications only
  • Some developers may find the API less intuitive compared to more traditional state management patterns

Code Examples

  1. Creating an atom (basic state unit):
import { atom } from 'recoil';

const counterState = atom({
  key: 'counterState',
  default: 0,
});
  1. Using an atom in a component:
import { useRecoilState } from 'recoil';

function Counter() {
  const [count, setCount] = useRecoilState(counterState);
  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}
  1. Creating a selector (derived state):
import { selector } from 'recoil';

const doubleCountState = selector({
  key: 'doubleCountState',
  get: ({get}) => {
    const count = get(counterState);
    return count * 2;
  },
});

Getting Started

To start using Recoil in your React project:

  1. Install Recoil:

    npm install recoil
    
  2. Wrap your app with RecoilRoot:

    import { RecoilRoot } from 'recoil';
    
    function App() {
      return (
        <RecoilRoot>
          {/* Your app components */}
        </RecoilRoot>
      );
    }
    
  3. Create and use atoms and selectors in your components as shown in the code examples above.

Competitor Comparisons

Official React bindings for Redux

Pros of react-redux

  • Well-established ecosystem with extensive documentation and community support
  • Predictable state management with a single source of truth
  • Time-travel debugging and powerful developer tools

Cons of react-redux

  • Steeper learning curve due to concepts like reducers, actions, and middleware
  • More boilerplate code required for setup and state updates
  • Can be overkill for smaller applications or simpler state management needs

Code Comparison

react-redux:

const mapStateToProps = state => ({ todos: state.todos })
const mapDispatchToProps = { addTodo }
export default connect(mapStateToProps, mapDispatchToProps)(TodoList)

Recoil:

const todoListState = atom({ key: 'todoListState', default: [] })
function TodoList() {
  const [todos, setTodos] = useRecoilState(todoListState)
  // ...
}

Key Differences

  • Recoil uses a more React-like approach with hooks and atoms
  • react-redux requires explicit connection of components to the store
  • Recoil offers easier code splitting and dynamic state management
  • react-redux provides a more structured approach to state updates

Both libraries aim to solve state management in React applications, but Recoil focuses on simplicity and flexibility, while react-redux emphasizes predictability and scalability for larger applications.

27,458

Simple, scalable state management.

Pros of MobX

  • More mature and battle-tested, with a larger ecosystem and community support
  • Provides a simpler, more straightforward API for state management
  • Offers better performance for large-scale applications with complex state

Cons of MobX

  • Less React-specific, which may lead to integration challenges in some cases
  • Requires more boilerplate code for setting up observables and actions
  • Learning curve can be steeper for developers new to reactive programming concepts

Code Comparison

MobX:

import { makeObservable, observable, action } from "mobx";

class TodoStore {
  todos = [];

  constructor() {
    makeObservable(this, {
      todos: observable,
      addTodo: action
    });
  }

  addTodo(text) {
    this.todos.push({ text, completed: false });
  }
}

Recoil:

import { atom, selector } from "recoil";

const todoListState = atom({
  key: "todoListState",
  default: []
});

const addTodoSelector = selector({
  key: "addTodoSelector",
  set: ({ get, set }, newTodo) => {
    const todoList = get(todoListState);
    set(todoListState, [...todoList, newTodo]);
  }
});
60,792

A JS library for predictable global state management

Pros of Redux

  • Well-established ecosystem with extensive tooling and middleware
  • Predictable state management with a single source of truth
  • Time-travel debugging and easy state persistence

Cons of Redux

  • Boilerplate code required for setup and actions
  • Steep learning curve for beginners
  • Can be overkill for small to medium-sized applications

Code Comparison

Redux:

const counterReducer = (state = 0, action) => {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1
    default:
      return state
  }
}

Recoil:

const counterState = atom({
  key: 'counterState',
  default: 0,
})

const Counter = () => {
  const [count, setCount] = useRecoilState(counterState)
}

Redux requires defining reducers and actions, while Recoil uses atoms for simpler state management. Redux's approach provides more structure but can be verbose, whereas Recoil offers a more concise and React-like syntax.

Redux is better suited for large-scale applications with complex state management needs, while Recoil shines in React-centric projects where simplicity and ease of use are prioritized. The choice between them depends on project requirements, team expertise, and application complexity.

46,119

🐻 Bear necessities for state management in React

Pros of zustand

  • Simpler API with less boilerplate code
  • Smaller bundle size and faster performance
  • Easier to integrate with existing projects

Cons of zustand

  • Less powerful for complex state management scenarios
  • Fewer built-in features for handling asynchronous operations
  • Limited ecosystem compared to Recoil

Code Comparison

Zustand:

import create from 'zustand'

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

Recoil:

import { atom, useRecoilState } from 'recoil'

const countState = atom({
  key: 'countState',
  default: 0,
})

function Counter() {
  const [count, setCount] = useRecoilState(countState)
  const increment = () => setCount(count + 1)
}

Both zustand and Recoil are state management libraries for React applications. zustand offers a simpler API and better performance, making it easier to integrate into existing projects. However, Recoil provides more powerful features for complex state management scenarios and has a larger ecosystem. The code comparison shows that zustand requires less setup and boilerplate, while Recoil offers a more structured approach to state management.

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

Recoil · NPM Version Node.js CI GitHub license Follow on Twitter

Recoil is an experimental state management framework for React.

Website: https://recoiljs.org

Documentation

Documentation: https://recoiljs.org/docs/introduction/core-concepts

API Reference: https://recoiljs.org/docs/api-reference/core/RecoilRoot

Tutorials: https://recoiljs.org/resources

Installation

The Recoil package lives in npm. Please see the installation guide

To install the latest stable version, run the following command:

npm install recoil

Or if you're using yarn:

yarn add recoil

Or if you're using bower:

bower install --save recoil

Contributing

Development of Recoil happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving Recoil.

License

Recoil is MIT licensed.

NPM DownloadsLast 30 Days