All Projects → skt-t1-byungi → use-simple-store

skt-t1-byungi / use-simple-store

Licence: MIT License
🏬 Simple state management using React Hook

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to use-simple-store

react-without-redux
React State Management without Redux
Stars: ✭ 33 (+57.14%)
Mutual labels:  state-management, react-hooks
resynced
An experimental hook that lets you have multiple components using multiple synced states using no context provider
Stars: ✭ 19 (-9.52%)
Mutual labels:  state-management, react-hooks
use-state-machine
Use Finite State Machines with React Hooks
Stars: ✭ 28 (+33.33%)
Mutual labels:  state-management, react-hooks
react-immer
No nonsense state management with Immer and React hooks
Stars: ✭ 13 (-38.1%)
Mutual labels:  state-management, react-hooks
jedisdb
redis like key-value state management solution for React
Stars: ✭ 13 (-38.1%)
Mutual labels:  state-management, react-hooks
react-mlyn
react bindings to mlyn
Stars: ✭ 19 (-9.52%)
Mutual labels:  state-management, react-hooks
east-store
east-store is a state manager with easiest api that based hooks and immer.
Stars: ✭ 18 (-14.29%)
Mutual labels:  state-management, react-hooks
react-cool-form
😎 📋 React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (+1071.43%)
Mutual labels:  state-management, react-hooks
use-query-string
🆙 A React hook that serializes state into the URL query string
Stars: ✭ 50 (+138.1%)
Mutual labels:  state-management, react-hooks
concave
🧐 Lens-like state management (for React).
Stars: ✭ 13 (-38.1%)
Mutual labels:  state-management, react-hooks
rex-state
Convert hooks into shared states between React components
Stars: ✭ 32 (+52.38%)
Mutual labels:  state-management, react-hooks
atomic-state
A decentralized state management library for React
Stars: ✭ 54 (+157.14%)
Mutual labels:  state-management, react-hooks
Easy Peasy
Vegetarian friendly state for React
Stars: ✭ 4,525 (+21447.62%)
Mutual labels:  state-management, react-hooks
niue
A tiny shared state and event library for React
Stars: ✭ 17 (-19.05%)
Mutual labels:  state-management, react-hooks
Constate
React Context + State
Stars: ✭ 3,519 (+16657.14%)
Mutual labels:  state-management, react-hooks
use-app-state
🌏 useAppState() hook. that global version of setState() built on Context.
Stars: ✭ 65 (+209.52%)
Mutual labels:  state-management, react-hooks
hookstore
Hook based and lightweight centralized state management for React.
Stars: ✭ 28 (+33.33%)
Mutual labels:  state-management, react-hooks
react-store
A library for better state management in react hooks world
Stars: ✭ 34 (+61.9%)
Mutual labels:  state-management, react-hooks
React-Chat-App
This is a real time chat application with features of online status,read receipnt,image support and create multiple rooms
Stars: ✭ 13 (-38.1%)
Mutual labels:  react-hooks
ember
@microstates/ember - Official Microstates bindings for Ember.js
Stars: ✭ 68 (+223.81%)
Mutual labels:  state-management

use-simple-store 🏬

Simple global state management using react hook.

npm travis license

Install

npm i use-simple-store

Example

import createStore from 'use-simple-store'

const { useStore, update } = createStore({ count: 0 })

const increment = () => update(state => {
    state.count = state.count + 1
})

const decrement = () => update(state => {
    state.count = state.count - 1
})

function App() {
    const { count } = useStore()

    return (
        <div>
            <span>{count}</span>
            <button onClick={decrement}> - </button>
            <button onClick={increment}> + </button>
        </div>))
}

API

createStore(initialState)

Create a store.

store.update([mutate])

Update state by mutate function. (using immer)

const todos = createStore({})

const addTodo = (id, text) => todos.update(state => {
    state[id] = { id, text, done: false }
})

const deleteTodo = id => todos.update(state => {
    delete state[id]
})

Asynchronous update.

The update() does not support promise. If you need an asynchronous update, see the example below: 👇

async function fetchTodos() {
    update(state => {
        state.fetching = true
    })

    const todos = await fetchTodosAsync()

    update(state => {
        state.fetching = false
        state.todos = todos
    })
}

store.subscribe(listener)

Subscribe to state changes.

const { subscribe, update } = createStore({count: 0})

subscribe(state => {
    console.log(`count: ${state.count}`)
})

update(state => { state.count++ }) // => count: 1
update(state => { state.count++ }) // => count: 2

Unsubscribe

The subscribe() returns a function to unsubscribe.

const unsubscribe = subscribe(listener)

/* ... */

unsubscribe()

store.getState()

Returns current state.

store.useStore([selector[, dependencies]])

A react hook to subscribe to store state changes.

selector

Select only the necessary state of the store. When the state of the store is large, its performance is better.

function Todo({ id }) {
    const todo = useStore(state => state[id])

    const handleClick = () => {
        update(state => {
            state[id].done = !todo.done
        })
    }

    return <li onClick={handleClick}>{todo.text}</li>)
}

dependencies

Replace the selector with dependencies.

const s = useStore(state => state[condition ? 'v1':'v2'], [condition])

License

MIT

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].