useAppState() hook. that global version of setState() built on useContext.
!CI !npm !minizip    
> 🌏 useAppState() hook. that global version of setState() built on useContext.
- 😀 Usage
- 🤔 Why
- 📺 Demo
- 💾 Installation
- 🛠 API
-
- [const [appState, setAppState] = useAppState()](#const-appstate-setappstate--useappstate)
- Get value from appState
- update appState with setAppState(appState: Object)
- 📕 TypeScript
- Example
- LICENSE
- Contributors
``jsx
// 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(
document.getElementById('root')
)
function App() {
const [appState, setAppState] = useAppState()
const increment = () => setAppState({ count: appState.count + 1 })
const decrement = () => setAppState({ count: appState.count - 1 })
return (
I have {appState.count} apples
🤔 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

> github: https://github.com/ryota-murakami/use-app-state-example
💾 Installation
`sh
npm install @laststance/use-app-state
`or
`sh
yarn add @laststance/use-app-state
`🛠 API
$3
- Make your AppState as a plain Javascript Object.(eg:
const AppState = {foo: "bar"})
- Wrap Provider in your root app component.`jsx
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(
,
document.getElementById('root')
)
`$3
- Gives interface to access and set the global appState.
##### Get value from
appState`jsx
import { useAppState } from '@laststance/use-app-state'const AppleComponent = () => {
const [appState, setAppState] = useAppState()
return
{appState.thisIsMyValue}
}
`##### update appState with
setAppState(appState: Object)`jsx
import { useAppState } from '@laststance/use-app-state'const NintendoComponent = () => {
const [appState, setAppState] = useAppState()
const orderSmashBros = () => setAppState({ sales: appState.sales + 1 })
return
}
`📕 TypeScript
This package contains an
index.d.ts type definition file, so you can use it in TypeScript without extra configuration.$3
`tsx
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() // 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) =>
{f}
)return (
{foodListView})
}ReactDOM.render(
,
document.getElementById('root')
)
``- use-app-state-typescript-todo-example
MIT
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!