A lightweight Entity Component System (ECS) implementation for React applications. This architecture allows React components to serve purely as renderers, while all state management and business logic is handled by the ECS pattern.
This provides a type-safe and scalable way to manage complex state and behavior, particularly useful for any React application that benefits from decoupled state and rendering logic.
npm install ecs-react
# or
yarn add ecs-reactUnlike traditional state management solutions like Redux or MobX, which organize state around a global store or observable objects, ecs-react takes a fundamentally different approach:
- Composition over Global State: Instead of a global state tree, state is organized around entities and their components, making it easier to add/remove features dynamically
- Behavior Separation: Systems handle logic independently of data (components) and presentation (React components), creating cleaner separation than reducer/action patterns
ecs-react implements the Entity Component System (ECS) pattern, which consists of:
- Entities: Base objects that serve as containers for components
- Components: Pure data containers that define the properties of entities
- Systems: Logic that operates on entities with specific components
- Signals: Events that trigger system behaviors
👉 Complete example can be found here: examples/ecs-react-example
First, define your Component and Signal schemas:
import { ComponentSchema, SignalSchema } from "ecs-react";
// Define all your Component types
type MyComponents = ComponentSchema<{
counter: { count: number };
label: string;
}>;
// Define all possible Signal types
type MySignals = SignalSchema<"click" | "hover">;Create a typed instance of the ECS:
import { ECS } from "ecs-react";
const ecs = new ECS<MyComponents, MySignals>();Entities are created and managed through the ECS instance:
// Create a new entity
const counterEntity = ecs.createEntity();
// Add components to the entity
counterEntity.addComponent("counter", { count: 0 });
counterEntity.addComponent("lastUpdatedAt", { count: Date.now() });Systems define behavior that operates on entities when specific signals are received:
ecs.createSystem({
name: "counterSystem",
signals: ["click"],
fn: ({ entity }) => {
entity.updateComponent("counter", prevValue => ({
count: prevValue.count + 1,
}));
},
});The above system will run when the click signal is received.
React components can interact with entities using the provided hooks:
import { useComponent, WithEntityProps } from "ecs-react";
// Button that executes the click signal on the counter entity
const Button: React.FC<WithEntityProps<MyComponents>> = ({ entity }) => {
const onClick = () => ecs.signal("click", entity);
return <button onClick={onClick}>Click</button>;
};
// Display component that shows the counter entity
const Display: React.FC<WithEntityProps<MyComponents>> = ({ entity }) => {
// internally subscribes to updates on the counter component of the entity
const counter = useComponent(entity, "counter");
return <span>{counter?.count}</span>;
};Post any issues and suggestions on the GitHub issues page. To contribute, fork the project and then create a pull request back to main.