GithubHelp home page GithubHelp logo

laststance / use-app-state Goto Github PK

View Code? Open in Web Editor NEW
66.0 5.0 7.0 6.59 MB

๐ŸŒ useAppState() hook. that global version of setState() built on Context.

License: MIT License

JavaScript 7.51% TypeScript 92.49%
react hooks npm state-management context react-contect typescript appstate react-hooks custom-hook

use-app-state's Introduction

A Note from the Author (@ryota-murakami, Oct 2 2022):

Hi! I created this library four years ago, to simplify the passing of values between components in prototyping.
But This lib doesn't support React v18's Concurrent Rendering and remaining performance issue.
So i deprecate this lib and reccomends other state management library like Redux Toolkit, Jotai.
I thank all the users and contributors of this library, thank you ever so much.


@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!

use-app-state's People

Contributors

allcontributors[bot] avatar dependabot[bot] avatar depfu[bot] avatar gnasamx avatar jackhedaya avatar nake89 avatar ryota-murakami avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

use-app-state's Issues

Build Demo App that introduce I expecting the library use case effective

Significant is not expected used to remote fetch data store.

  • use modern api client that remote data fetch and manage that data like SWR
  • manage window your interaction state in use-app-state like
    • agree use cookie window
    • theme color change
    • language change
    • display snack bar
    • display entire screen spinner
    • time exclusive info bar that show one time until user click โŒ button
    • tutorial modal
    • last opened user view
    • notification state(isRead)

Replace old CodeSandox DEMO

The npm was different name about a year ago.
That times DEMO still working today, so I want to re-create from this repositoty.

Migrate to TypeScript

Why

  • flow version up's breaking change are painful
  • TypeScript project can be auto generate index.d.ts

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.