All Projects → pie6k → hooksy

pie6k / hooksy

Licence: MIT license
Simple app state management based on react hooks

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to hooksy

micro-observables
A simple Observable library that can be used for easy state management in React applications.
Stars: ✭ 78 (+34.48%)
Mutual labels:  hooks, state-management, mobx
React Atom
A simple way manage state in React, inspired by Clojure(Script) and reagent.cljs
Stars: ✭ 133 (+129.31%)
Mutual labels:  hooks, state-management, mobx
React Hooks Mobx State Tree
React Hooks + MobX State Tree + TypeScript = 💛
Stars: ✭ 169 (+191.38%)
Mutual labels:  hooks, mobx
Alldemo
🍑 2020全栈学习Demo大合集 包含最新 hooks TS 等 还有umi+dva,数据可视化等实战项目 (持续更新中)
Stars: ✭ 189 (+225.86%)
Mutual labels:  hooks, mobx
mst-effect
💫 Designed to be used with MobX-State-Tree to create asynchronous actions using RxJS.
Stars: ✭ 19 (-67.24%)
Mutual labels:  state-management, mobx
Redhooks
Predictable state container for React apps written using Hooks
Stars: ✭ 149 (+156.9%)
Mutual labels:  hooks, state-management
React Model
The next generation state management library for React
Stars: ✭ 153 (+163.79%)
Mutual labels:  hooks, state-management
Reusable
Reusable is a library for state management using React hooks
Stars: ✭ 207 (+256.9%)
Mutual labels:  hooks, state-management
Use Substate
🍙 Lightweight (<600B minified + gzipped) React Hook to subscribe to a subset of your single app state.
Stars: ✭ 97 (+67.24%)
Mutual labels:  hooks, state-management
dobux
🍃 Lightweight responsive state management solution.
Stars: ✭ 75 (+29.31%)
Mutual labels:  hooks, state-management
NObservable
MobX like observable state management library with Blazor support
Stars: ✭ 66 (+13.79%)
Mutual labels:  state-management, mobx
useSharedState
useSharedState is a simple hook that can be used to share state between multiple React components.
Stars: ✭ 0 (-100%)
Mutual labels:  hooks, state-management
Pure Store
A tiny immutable store with type safety.
Stars: ✭ 133 (+129.31%)
Mutual labels:  hooks, state-management
Use Url State
Lift a React component's state into the url
Stars: ✭ 154 (+165.52%)
Mutual labels:  hooks, state-management
Clean State
🐻 A pure and compact state manager, using React-hooks native implementation, automatically connect the module organization architecture. 🍋
Stars: ✭ 107 (+84.48%)
Mutual labels:  hooks, mobx
Reto
Flexible and efficient React Store with hooks.
Stars: ✭ 194 (+234.48%)
Mutual labels:  hooks, state-management
resynced
An experimental hook that lets you have multiple components using multiple synced states using no context provider
Stars: ✭ 19 (-67.24%)
Mutual labels:  hooks, state-management
Use Global Context
A new way to use “useContext” better
Stars: ✭ 34 (-41.38%)
Mutual labels:  hooks, state-management
React Context Hook
A React.js global state manager with Hooks
Stars: ✭ 50 (-13.79%)
Mutual labels:  hooks, state-management
ngx-mobx
Mobx decorators for Angular Applications
Stars: ✭ 14 (-75.86%)
Mutual labels:  state-management, mobx

Hooksy

Hooksy is state managment solution based on react hooks

  • extremly easy to use
  • no boilerplate
  • works great with typescript
  • composable
  • injectable to any component with 1 line of code
  • allows creating complex data solutions (like actions, selectors) intuitively

demo

Tutorial

The best way to show how it works would be by example.

Let's assume we've got user store that is used by many components across the page. You can log in and out.

First - let's create the store:

// userStore.ts
import { createStore } from 'hooksy';

interface UserData {
  username: string;
}

const defaultUser: UserData = { username: 'Foo' };

export const [useUserStore] = createStore(defaultUser); // we've created store with initial value.
// useUserStore has the same signature like react useState hook, but the state will be shared across all components using it

Our store is ready and can be used inside any react component. If any component will modify user store state - all components using it will be re-rednered.

Let's see how we can use the store now:

import React from 'react';

import { useUserStore } from './userStore';

export function UserInfo() {
  const [user, setUser] = useUserStore();
  
  function login() {
    setUser({ username: 'Foo' })
  }

  return (
    <div>
      {!user && <strong>You're logged out<button onPress={login}>Login</button></strong>}
      {user && <strong>Logged as <strong>{user.username}</strong></strong>}
    </div>
  );
}

Now, we can also use the same store in totally different component:

import React from 'react';

import { useUserStore } from './userStore';

export function Footer() {
  const [user, setUser] = useUserStore();
  
  function logout() {
    setUser(null)
  }

  return (
    <div>
      {user && <strong>Press this button to log out: <button onPress={logout}>Log out</button></strong>}
    </div>
  );
}

If the logout button is pressed, both components using user store will get updated.

As you might notice - we've implemented login and logout actions inside components which might not be good idea. We can easily avoid that by creating custom hooks with all needed actions (and selectors or anything javascript will allow you to write)

Let's modify our user store definition

import { useCallback } from 'react';

import { createStore } from '../../src';

interface UserData {
  username: string;
}

export const [useUserStore] = createStore<UserData>(null); // user store is defined the same way as before

// custom hook 
export function useUser() {
  const [user, setUser] = useUserStore();

  // let's define custom actions (we can - and should - use useCallback hooks - later on)
  function logout() {
    setUser(null);
  }

  function login(username: string) {
    setUser({username})
  }

  return {
    user,
    logout,
    login,
  };
}

Now, we can modify our components eg:

import React from 'react';

import { useUser } from './userStore';

export function UserInfo() {
  const { user, login } = useUser();

  return (
    <div>
      {!user && <strong>You're logged out<button onPress={() => login({ username: 'Foo' })}>Login</button></strong>}
      {user && <strong>Logged as <strong>{user.username}</strong></strong>}
    </div>
  );
}

Avoiding re-renders when they're not needed

Let's say username is case insensitive and we want to re-render some component only when username is really changed eg 'Foo' changed to 'foo' should NOT cause re-render

To do that, we can use user store hook like

const [user, setUser] = useUserStore({ 
  shouldUpdate(oldUser, newUser) {
    return oldUser.username.toLowerCase() !== newUser.username.toLowerCase()
  }
})

Modify store state from outside of any component Let's say you want to log out user when some external event occurs. eg browser window get's closed etc

To do that, you can:

// userStore.ts
import { createStore } from 'hooksy';

interface UserData {
  username: string;
}

const defaultUser: UserData = { username: 'Foo' };

// NOTE 2nd tuple element allows us to update user store anywhere (and all components using it will get re-rendered)
export const [useUserStore, updateUser] = createStore(defaultUser); // we've created store with initial value.

// later on you can use it like
onSomeCustomBrowserEvent(() => {
  updateUser(null);
})
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].