All Projects β†’ laststance β†’ use-app-state

laststance / use-app-state

Licence: MIT license
🌏 useAppState() hook. that global version of setState() built on Context.

Programming Languages

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

Projects that are alternatives of or similar to use-app-state

react-cool-form
😎 πŸ“‹ React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (+278.46%)
Mutual labels:  hooks, state-management, react-hooks
use-query-string
πŸ†™ A React hook that serializes state into the URL query string
Stars: ✭ 50 (-23.08%)
Mutual labels:  hooks, state-management, react-hooks
resynced
An experimental hook that lets you have multiple components using multiple synced states using no context provider
Stars: ✭ 19 (-70.77%)
Mutual labels:  hooks, state-management, react-hooks
atomic-state
A decentralized state management library for React
Stars: ✭ 54 (-16.92%)
Mutual labels:  hooks, state-management, react-hooks
Easy Peasy
Vegetarian friendly state for React
Stars: ✭ 4,525 (+6861.54%)
Mutual labels:  hooks, state-management, react-hooks
Constate
React Context + State
Stars: ✭ 3,519 (+5313.85%)
Mutual labels:  hooks, state-management, react-hooks
jedisdb
redis like key-value state management solution for React
Stars: ✭ 13 (-80%)
Mutual labels:  hooks, state-management, react-hooks
Use Global Context
A new way to use β€œuseContext” better
Stars: ✭ 34 (-47.69%)
Mutual labels:  hooks, state-management, context
flhooks
React like Hooks implementation for Flutter.
Stars: ✭ 38 (-41.54%)
Mutual labels:  hooks, custom-hook, react-hooks
react-ui-hooks
🧩Simple repository of React hooks for building UI components
Stars: ✭ 20 (-69.23%)
Mutual labels:  hooks, react-hooks
react-generate-context
A helper function for reducing React Context boilerplate
Stars: ✭ 24 (-63.08%)
Mutual labels:  hooks, context
dobux
πŸƒ Lightweight responsive state management solution.
Stars: ✭ 75 (+15.38%)
Mutual labels:  hooks, state-management
niue
A tiny shared state and event library for React
Stars: ✭ 17 (-73.85%)
Mutual labels:  state-management, react-hooks
react-mlyn
react bindings to mlyn
Stars: ✭ 19 (-70.77%)
Mutual labels:  state-management, react-hooks
hooksy
Simple app state management based on react hooks
Stars: ✭ 58 (-10.77%)
Mutual labels:  hooks, state-management
react-immer
No nonsense state management with Immer and React hooks
Stars: ✭ 13 (-80%)
Mutual labels:  state-management, react-hooks
react-use-comlink
Three ways to use Comlink web workers through React Hooks (and in a typesafe manner).
Stars: ✭ 39 (-40%)
Mutual labels:  hooks, react-hooks
rex-state
Convert hooks into shared states between React components
Stars: ✭ 32 (-50.77%)
Mutual labels:  state-management, react-hooks
react-without-redux
React State Management without Redux
Stars: ✭ 33 (-49.23%)
Mutual labels:  state-management, react-hooks
useCustomHooks
πŸ“¦ npm package containing a set of custom hooks for your next React project.
Stars: ✭ 12 (-81.54%)
Mutual labels:  hooks, custom-hook

@laststance/use-app-state

CI npm minizip Depfu tested with jest code style: prettier All Contributors

🌏 useAppState() hook. that global version of setState() built on useContext.

πŸ˜€ Usage

// index.js
import React from 'react'
import ReactDOM from 'react-dom'
import AppStateRoot, { useAppState } from '@laststance/use-app-state'

// initialState must be Plain Object
const initialState = { count: 0 }

ReactDOM.render(
  <AppStateRoot initialState={initialState}>
    <App />
  </AppStateRoot>,
  document.getElementById('root')
)

function App() {
  const [appState, setAppState] = useAppState()

  const increment = () => setAppState({ count: appState.count + 1 })
  const decrement = () => setAppState({ count: appState.count - 1 })

  return (
    <div>
      <button onClick={increment}>increment</button>
      <button onClick={decrement}>decrement</button>
      <p>I have {appState.count} apples </p>
    </div>
  )
}

πŸ€” Why

I wanted just setState() but can use across the another components for prototyping.

There is no special things against generally common kind of useContext() hook based global store.
Therefore you have to apply some technique if you want to be thorough ultimate performance tune.

πŸ“Ί Demo

Edit @laststance/use-app-state Example

codesandbox

github: https://github.com/ryota-murakami/use-app-state-example

πŸ’Ύ Installation

npm install @laststance/use-app-state

or

yarn add @laststance/use-app-state

πŸ›  API

<Provider initialState={AppState} />

  • Make your AppState as a plain Javascript Object.(eg: const AppState = {foo: "bar"})
  • Wrap Provider in your root app component.
import /* Provider is default exported. So any available whatever you want */ StateWrapper from '@laststance/use-app-state'

// initialAppState must be Plain Object
const initialState = { count: 0 }

ReactDOM.render(
  <StateWrapper initialState={initialState}>
    <App />
  </StateWrapper>,
  document.getElementById('root')
)

const [appState, setAppState] = useAppState()

  • Gives interface to access and set the global appState.
Get value from appState
import { useAppState } from '@laststance/use-app-state'

const AppleComponent = () => {
  const [appState, setAppState] = useAppState()

  return <div>{appState.thisIsMyValue}</div>
}
update appState with setAppState(appState: Object)
import { useAppState } from '@laststance/use-app-state'

const NintendoComponent = () => {
  const [appState, setAppState] = useAppState()
  const orderSmashBros = () => setAppState({ sales: appState.sales + 1 })

  return <button onClick={orderSmashBros}>You can not wait!!</button>
}

πŸ“• TypeScript

This package contains an index.d.ts type definition file, so you can use it in TypeScript without extra configuration.

Example

import React, { ReactElement } from 'react'
import ReactDOM from 'react-dom'
import Provider, { useAppState } from '@laststance/use-app-state'

interface Food {
  id: string
  name: string
}

type FoodList = Food[]

interface AppState {
  FoodList: FoodList
}

let initialAppState: AppState = {
  foodList: []
}

const App = () => {
const [appState, setAppState] = useAppState<AppState>() // pass appState object type as a generics
const item1: Food = {id: 'j4i3t280u', name: 'Hamburger'}
const item2: Food = {id: 'f83ja0j2t', name: 'Fried chicken'}
setAppState({foodList: [item1, item2]})

const foodListView: ReactElement[] = appState.foodList.map((f: Food) => <p key={f.id}>{f}</p>)

return (<div>{foodListView}</div>)
}

ReactDOM.render(
    <Provider initialState={initialAppState}>
      <App>
    </Provider>,
  document.getElementById('root')
)

LICENSE

MIT

Contributors

Thank you to all these wonderful people (emoji key): I want to improve this library (especially stability) and your contribution is so helpful!


ryota-murakami

πŸ’» πŸ“– ⚠️

Jack Hedaya

πŸ“–

Ganesh Pawar

πŸ“–

Kevin Kivi

πŸ“–

This project follows the all-contributors specification. Contributions of any kind are welcome!

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