Skip to content
This repository was archived by the owner on Oct 2, 2022. It is now read-only.
/ use-app-state Public archive

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

License

Notifications You must be signed in to change notification settings

laststance/use-app-state

Folders and files

NameName
Last commit message
Last commit date

Latest commit

88af091 Β· Dec 1, 2019
Oct 29, 2019
Oct 29, 2019
Nov 4, 2019
Dec 1, 2019
Oct 29, 2019
Aug 20, 2019
Jul 2, 2019
Oct 29, 2019
Nov 12, 2019
Oct 29, 2019
Oct 29, 2019
Feb 16, 2019
Oct 25, 2019
Nov 4, 2019
Oct 29, 2019
Dec 1, 2019
Oct 29, 2019
Nov 30, 2019

Repository files navigation

React AppState CircleCI tested with jest jest code style: prettier All Contributors Netlify Status

🌏 React Global AppState for Sharing Data between each components Built on Context.

top

Usage

// index.js
import React, { Fragment } from 'react'
import ReactDOM from 'react-dom'
import Provider, { useAppState } from '@ryotamurakami/react-appstate'
 
// initialAppState must be Plain Object
const initialAppState = { count: 0 }
 
ReactDOM.render(
  <Provider appState={initialAppState}>
    <App />
  </Provider>,
  document.getElementById('root')
)
 
function App() {
  const [appState, setAppState] = useAppState()

  return (
    <Fragment>
      <div>
        <button onClick={() => setAppState({ count: appState.count + 1 })}>increment</button>
        <button onClick={() => setAppState({ count: appState.count - 1 })}>decrement</button>
      </div>
      <p>I have {appState.apple.count} apples </p>
    </Fragment>
  )
}

Play πŸ‘‡

Edit react-appstate-exampe

codesandbox

https://react-appstate-demo.netlify.com/ Same code as above usage's one.

Why

I wanted a sharable state over the component hierarchy that could be setup immediately (in one shot). The goal was to have something similar to a global version of setState() with a simple interface.

Although there are many similar libraries and blog posts with code examples, they tended to be unnecessarily complicated / difficult to reuse. React AppState is awesome for prototyping, experimenting, and developing small apps.

Now, thesetAppState() custom hook is packed it as an npm package to make setup one shot anywhere! 🍸

Resources

Installation

npm install @ryotamurakami/react-appstate

API

<Provider appState={AppState} />

  • Make your AppState as a plain Javascript Object.(eg: const GlobalStaate = {foo: "bar"})
  • Wrap Provider in your root app component.
import Provider from '@ryotamurakami/react-appstate'

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

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

const [appState, setAppState] = useAppState()

  • Gives interface to access and set the global appState.

Get value from appState

// example
import { useAppState } from '@ryotamurakami/react-appstate'

const AppleComponent = () => {
  const [appState, setAppState] = useAppState()
  
  return (<div><{appState.thisIsMyValue}/div>)
}

update appState with setAppState(appState: Object)

// example
import { useAppState } from '@ryotamurakami/react-appstate'

const NintendoComponent = () => {
  const [appState, setAppState] = useAppState()
  const orderSmashBros = () => setAppState({sales: appState.sales + 1 })
  
  return (<button onClick={orderSmashBros}>You can not wait!!</button>)
}

Advanced

This is an abstract example utilizing custom Hooks.

  • src/index.js
import React from 'react'
import ReactDOM from 'react-dom'
import Provider, { useAppState } from '@ryotamurakami/react-appstate'
import { Layout } from './style'
import useAction from './actions'

const initialAppState = { count: 0 }
ReactDOM.render(
  <Provider appState={initialAppState}>
    <App />
  </Provider>,
  document.getElementById('root')
)

function App() {
  const action = useAction()
  return (
    <Layout>
      <div>
        <button onClick={action.increment}>increment</button>
        <button onClick={action.decrement}>decrement</button>
      </div>
      <p>{useAppState().appState.count}</p>
    </Layout>
  )
}
  • src/actions.js
import { useAppState } from '@ryotamurakami/react-appstate'

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

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

  return Action
}

export default useAction

Multiple AppStates

・Play πŸ‘‡

Edit react-appstate-multiple-appState-example

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 '@ryotamurakami/react-appstate'

interface Food {
  id: string
  name: string
}

type TodoList = Todo[]

interface AppState {
  FoodList: FoodList
}

let initialAppState: AppState = {
  foodList: []
}

const App = () => {
const [appState, setAppState] = useAppState<AppState>() // pass appState object type as generic
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 appState={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
ryota-murakami

πŸ’» πŸ“– ⚠️
Jack Hedaya
Jack Hedaya

πŸ“–

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