hookstate
The simple but very powerful and incredibly fast state management for React that is based on hooks
Top Related Projects
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.
🐻 Bear necessities for state management in React
Simple, scalable state management.
The official, opinionated, batteries-included toolset for efficient Redux development
🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query.
Quick Overview
Hookstate is a lightweight and flexible state management library for React applications. It provides a simple and intuitive API for managing local and global state, with support for complex nested structures and efficient updates.
Pros
- Simple and intuitive API, easy to learn and use
- Excellent TypeScript support with strong typing
- Efficient updates and re-rendering optimization
- Supports both local and global state management
Cons
- Relatively smaller community compared to more established state management libraries
- Limited ecosystem of plugins and extensions
- May require additional setup for advanced features like middleware or devtools
Code Examples
Creating and using local state:
import { useState } from '@hookstate/core';
const Component = () => {
const state = useState(0);
return (
<div>
<p>Count: {state.get()}</p>
<button onClick={() => state.set(p => p + 1)}>Increment</button>
</div>
);
};
Managing nested state:
import { useState } from '@hookstate/core';
const state = useState({ user: { name: 'John', age: 30 } });
// Update nested property
state.user.name.set('Jane');
// Access nested value
console.log(state.user.name.get()); // 'Jane'
Using global state:
import { createState, useState } from '@hookstate/core';
const globalState = createState({ count: 0 });
const Component = () => {
const state = useState(globalState);
return (
<div>
<p>Global Count: {state.count.get()}</p>
<button onClick={() => state.count.set(p => p + 1)}>Increment</button>
</div>
);
};
Getting Started
To start using Hookstate in your React project:
-
Install the package:
npm install @hookstate/core
-
Import and use in your components:
import { useState } from '@hookstate/core'; const MyComponent = () => { const state = useState({ count: 0 }); return ( <div> <p>Count: {state.count.get()}</p> <button onClick={() => state.count.set(p => p + 1)}>Increment</button> </div> ); };
-
For global state, create a state outside of components and use it across your app:
import { createState, useState } from '@hookstate/core'; const globalState = createState({ user: null }); // In components: const state = useState(globalState);
Competitor Comparisons
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.
Pros of Recoil
- Developed and maintained by Facebook, ensuring robust support and updates
- Offers a more flexible and powerful approach to state management with atoms and selectors
- Provides built-in support for asynchronous data fetching and caching
Cons of Recoil
- Steeper learning curve due to its unique concepts and API
- Larger bundle size compared to Hookstate
- Still in experimental phase, which may lead to API changes and potential instability
Code Comparison
Recoil:
import { atom, useRecoilState } from 'recoil';
const counterState = atom({
key: 'counterState',
default: 0,
});
function Counter() {
const [count, setCount] = useRecoilState(counterState);
return <button onClick={() => setCount(count + 1)}>{count}</button>;
}
Hookstate:
import { createState, useState } from '@hookstate/core';
const globalState = createState(0);
function Counter() {
const state = useState(globalState);
return <button onClick={() => state.set(p => p + 1)}>{state.get()}</button>;
}
Both libraries offer simple and intuitive APIs for managing state in React applications. Recoil's approach with atoms and selectors provides more flexibility for complex state management scenarios, while Hookstate offers a more straightforward and lightweight solution for simpler use cases.
🐻 Bear necessities for state management in React
Pros of Zustand
- Simpler API with less boilerplate code
- Better TypeScript support out of the box
- More active community and frequent updates
Cons of Zustand
- Less powerful for complex state management scenarios
- Fewer built-in features for performance optimization
- Limited middleware options compared to Hookstate
Code Comparison
Zustand:
import create from 'zustand'
const useStore = create(set => ({
count: 0,
increment: () => set(state => ({ count: state.count + 1 })),
}))
Hookstate:
import { createState, useState } from '@hookstate/core'
const globalState = createState({ count: 0 })
const useGlobalState = () => useState(globalState)
Both Zustand and Hookstate are lightweight state management libraries for React applications. Zustand offers a simpler API and better TypeScript support, making it easier to get started with. However, Hookstate provides more advanced features for complex state management and performance optimization.
Zustand's approach is more straightforward, using a single create
function to define the store and its actions. Hookstate, on the other hand, separates the creation of global state and the hook for accessing it, which can be beneficial for larger applications with more complex state structures.
While Zustand has gained popularity due to its simplicity and active community, Hookstate offers more fine-grained control over state updates and nested state management. The choice between the two depends on the specific requirements of your project and personal preferences.
Simple, scalable state management.
Pros of MobX
- More mature and widely adopted, with a larger ecosystem and community support
- Provides automatic tracking of observables, reducing boilerplate code
- Offers flexible state management options, including classes and decorators
Cons of MobX
- Steeper learning curve, especially for developers new to reactive programming
- Can lead to less explicit data flow, potentially making debugging more challenging
- Requires additional configuration and setup, especially when used with TypeScript
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 });
}
}
Hookstate:
import { createState, useState } from "@hookstate/core";
const todoState = createState([]);
const TodoList = () => {
const state = useState(todoState);
const addTodo = (text) => state.merge([{ text, completed: false }]);
// ...
};
Both libraries offer state management solutions, but MobX provides a more traditional object-oriented approach, while Hookstate focuses on a hooks-based API. MobX's setup is more verbose but offers fine-grained control, whereas Hookstate's API is more concise and React-centric.
The official, opinionated, batteries-included toolset for efficient Redux development
Pros of Redux Toolkit
- Widely adopted and well-established in the React ecosystem
- Extensive documentation and community support
- Includes utilities for common Redux use cases, reducing boilerplate
Cons of Redux Toolkit
- Steeper learning curve for beginners
- More verbose code for simple state management scenarios
- Requires additional setup and configuration
Code Comparison
Redux Toolkit:
import { createSlice } from '@reduxjs/toolkit'
const counterSlice = createSlice({
name: 'counter',
initialState: 0,
reducers: {
increment: state => state + 1,
},
})
Hookstate:
import { createState, useState } from '@hookstate/core'
const globalState = createState(0)
const Counter = () => {
const state = useState(globalState)
return <button onClick={() => state.set(p => p + 1)}>{state.get()}</button>
}
Key Differences
- Redux Toolkit uses a centralized store and reducer approach, while Hookstate leverages React hooks for a more decentralized state management
- Hookstate offers a simpler API and less boilerplate for basic state management tasks
- Redux Toolkit provides more built-in tools for complex state management scenarios and middleware integration
Use Cases
- Redux Toolkit: Large-scale applications with complex state management needs and multiple developers
- Hookstate: Smaller to medium-sized projects or those prioritizing simplicity and ease of use in state management
🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query.
Pros of Query
- More robust caching and synchronization features
- Better support for server-side rendering
- Larger community and ecosystem
Cons of Query
- Steeper learning curve due to more complex API
- Potentially overkill for simpler state management needs
- Higher bundle size
Code Comparison
Query:
const { data, isLoading, error } = useQuery('todos', fetchTodos)
if (isLoading) return 'Loading...'
if (error) return 'An error occurred: ' + error.message
return (
<ul>{data.map(todo => <li key={todo.id}>{todo.title}</li>)}</ul>
)
Hookstate:
const todos = useHookstate(fetchTodos())
if (todos.promised) return 'Loading...'
if (todos.error) return 'An error occurred: ' + todos.error.message
return (
<ul>{todos.map(todo => <li key={todo.id}>{todo.get().title}</li>)}</ul>
)
Summary
Query excels in complex data fetching scenarios with its advanced caching and synchronization features, making it ideal for larger applications. It has a larger community and better server-side rendering support. However, it comes with a steeper learning curve and larger bundle size.
Hookstate offers a simpler API for basic state management, making it easier to learn and use for smaller projects. It has a smaller footprint but may lack some of the advanced features found in Query for complex data fetching and caching scenarios.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Hookstate
The most straightforward, extensible and incredibly fast state management that is based on React state hook.
Why? ⢠Docs / Samples ⢠Demo application ⢠Extensions ⢠Release notes
Preface
Hookstate is a modern alternative to Redux, Mobx, Recoil, etc. It is simple to learn, easy to use, extensible, very flexible and capable to address all state management needs of large scalable applications. It has got impressive performance and predictable behavior.
Any questions? Just ask by raising a GitHub ticket.
Why Hookstate
Migrating to version 4
hookstate.js.org/docs/migrating-to-v4
Documentation / Code samples / Demo applications
hookstate.js.org/docs/getting-started
Demo application
- Running: https://hookstate.js.org/docs/getting-started
- Source code: https://github.com/avkonst/hookstate/tree/master/docs/demos/todolist
Development tools
hookstate.js.org/docs/devtools
Plugins / Extensions
hookstate.js.org/docs/extensions-overview
API reference
hookstate.js.org/docs/typedoc-hookstate-core
Hookstate developers workflow
This is the mono repository, which combine the Hookstate core package, extensions, docs and demo applications. pnpm
is used as node_modules manager and nx
as a scripts launcher. Each package defines its own rules how to build, test, etc.
From the repository root directory:
-
npm install -f pnpm
- install pnpm tool -
pnpm install
- install node_modules for all packages -
pnpm nx <script> <package>
- run script for a package as well as build dependencies if required, for example:pnpm nx build core
- runbuild
script forcore
packagepnpm nx start todolist
- runstart
script fortodolist
package as well as build for all dependencies
Top Related Projects
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.
🐻 Bear necessities for state management in React
Simple, scalable state management.
The official, opinionated, batteries-included toolset for efficient Redux development
🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot