r/reactjs • u/Dense_Rub_620 • 4h ago
Show /r/reactjs React-style Hooks outside React
https://github.com/behavon/behavonRepository: https://github.com/behavon/behavon
What I wanted to build
What I find interesting about React is not only its declarative approach to describing UI, but also the way it expresses state and behavior through Hooks.
You declare state inside a function, derive other values from that state, and synchronize with external systems when dependencies change. State persists across function evaluations, and you can abstract this logic into custom Hooks.
But does this programming model necessarily have to be tied to UI components?
Could we express stateful domain objects, services, asynchronous tasks, and simulations in the same way?
Behavon is a library that implements this approach.
Hooks without React
Behavon follows the public state semantics of React Hooks but does not share a runtime with React.
Instead of using useState, useReducer, useMemo, useCallback, and useEffect inside React components, you use them inside functions called Models.
While React components return JSX, Behavon Models return arbitrary values.
A Model is instantiated through createModel and re-evaluated whenever its state changes. Its lifecycle can be managed independently of whether any UI is mounted.
Since Behavon has no runtime dependency on React, it can run both in the browser and in Node.js.
Modeling not just state, but behavior
One aspect I consider particularly important in Behavon is how relationships between Models are expressed.
For this purpose, Behavon introduces Ports, Signals, and Slots, similar to the concepts found in Qt.
- Port: A typed interface that a Model exposes to the outside world.
- Signal: An externally callable command or an event delivered to other Models.
- Slot: A receiving endpoint that responds to a Signal from another Model.
- Snapshot: An observable value that a Model exposes to the outside world.
Signals and Slots can be connected using useConnect.
You can also use useModel to observe the return value of another Model and build a Model that depends on that value.
In other words, dependencies between values and connections between behaviors can be expressed separately.
Rather than requiring Models to know each other's internal implementations, this structure allows them to communicate through their exposed interfaces. The lifecycle of these connections is also managed by the runtime.
This makes it possible to compose independent Models into an application.
How does it integrate with React?
React is not the execution environment for Behavon. It is simply one of the external consumers of Models.
A Model publishes the necessary values as Snapshots, and React subscribes to them through useSyncExternalStore.
When a user clicks a button, the UI calls a Model's Signal. The Model handles the state and behavior, and React reads the updated Snapshot.
This allows application logic to be executed and tested without a UI.
React server-side rendering (SSR) and hydration are currently not supported.
Features for expressing more complex behavior
I also implemented several additional features for problems that would be cumbersome to express using conventional Hooks and Model connections alone.
useFuture: Manages the progress and cancellation of asynchronous tasks. Tasks can be executed sequentially, or previous tasks can be cancelled in favor of the latest one.useResource: Manages the creation, closure, cancellation, and operations of external resources with their own lifecycles, such as WebSockets and database sessions.transaction: Groups state changes across multiple Models into a single transaction that can be committed or rolled back.useMachine: Defines and executes state transitions through rewrite rules.
In particular, useMachine is not limited to conventional finite state machines.
It supports structured terms, pattern matching, conditional rules, multisets, and rewriting of nested terms. Rules themselves can also be added, removed, or replaced at runtime.
This makes it possible to express computations within Models ranging from simple traffic lights and vending machines to Boolean expression simplification, lambda calculus, and forward-chaining inference.
What can you actually build with it?
Beyond a simple counter, the repository contains examples covering a variety of use cases:
- A to-do application with separate domain and presenter layers
- A team task management system composed of cooperating Models
- Search request cancellation and sequential file uploads
- WebSocket-based chat and database session management
- Bank transfers using transactions across multiple accounts
- A Node.js HTTP API server that runs without React
- A stock exchange simulation composed of independent Models representing companies, traders, portfolios, and an exchange
- Finite state machines, term rewriting, logical inference, and runtime rule modification
Examples involving interactions between multiple Models, such as the stock exchange simulation, illustrate that Behavon is not limited to storing UI state.
All examples use only the public API of the distributed package and include tests that verify their behavior.
Behavon also provides a dedicated ESLint plugin that checks Hook usage rules, dependency arrays, and the purity of Model evaluation.
Closing thoughts
What I am exploring with Behavon is the idea of separating the Hook programming model from the UI layer and using it to compose independent units of state and behavior.
I don't think every state management problem needs Behavon. Existing, simpler approaches are perfectly sufficient for simpler problems.
However, for applications in which multiple states and behaviors interact, and their respective lifecycles and interfaces need to be managed, I think this programming model could be another viable option.
You can install it with npm install behavon.
Repository: https://github.com/behavon/behavon
View all comments
Show parent comment
1
u/Dense_Rub_620 44m ago
The original motivation came from the direction React itself has been taking.
React has gradually shifted from component lifecycles to effect lifecycles.
Effects are now defined as a way to synchronize with external systems.
"You Might Not Need an Effect" is a good example of this direction.
I understand why this makes sense for a rendering engine.
However, I find Hooks to be a very expressive programming model for application logic as well.
This led me to a simple question.
If application logic should be independent of rendering, why should the Hooks programming model remain tied to a rendering engine?
I didn't want to give up Hooks just because I was moving logic outside React.
I wanted to give Hooks their own runtime.
One where state, effects, and lifecycles are driven by application behavior rather than UI rendering.
That's essentially why I started building Behavon.
Also, English isn't my native language.
So I used AI to help express my thoughts.
I hope you don't mind.