Effector React Bindings

23.3.0 · active · verified Sun Apr 19

effector-react provides the official React bindings for the effector state management library, allowing developers to seamlessly integrate effector's reactive, event-driven state model with their React applications. The current stable version is 23.3.0, frequently updated in parallel with the core effector library. Its key differentiators include a declarative, algebraic effects paradigm for managing complex application logic, which promotes clear separation of concerns between business logic and UI. It offers optimized React hooks, notably useUnit, for consuming effector stores, events, and effects, ensuring efficient component re-renders and simplified access to application state and actions. The library also boasts strong TypeScript support and recently added compatibility for React 19.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to create an Effector store and event, then consume them in a React functional component using the `useUnit` hook to manage form input state and display its length.

import { createStore, createEvent } from 'effector';
import { useUnit } from 'effector-react';
import React from 'react'; // Assuming React is imported for JSX

const inputText = createEvent<string>();

const $text = createStore<string>('').on(inputText, (_, text) => text);

const $size = $text.map(text => text.length);

const Form = () => {
  const { text, size } = useUnit({
    text: $text,
    size: $size,
  });
  const handleTextChange = useUnit(inputText);

  return (
    <form>
      <label htmlFor="text-input">Enter text:</label>
      <input
        id="text-input"
        type="text"
        onChange={e => handleTextChange(e.currentTarget.value)}
        value={text}
        aria-label="Text input for form"
      />
      <p>Length: {size}</p>
    </form>
  );
};

// To render this component (e.g., in App.js or index.js):
// import ReactDOM from 'react-dom/client';
// const root = ReactDOM.createRoot(document.getElementById('root'));
// root.render(<Form />);

view raw JSON →