GitXplorerGitXplorer
p

state

public
107 stars
1 forks
0 issues

Commits

List of commits on branch main.
Verified
a78eaf3d1d641040b2a293f5ae371e956db3e2b5

Update README.md

ppacocoursey committed 2 years ago
Verified
05db6fd0400f273005ba73dab87ba66ea13e0080

Update README.md

ppacocoursey committed 3 years ago
Verified
9bb2f48a27b69be32a9d94b5a3e81e480aff2aa8

Create context.js

ppacocoursey committed 3 years ago
Verified
bbf9203800469917ab58c8367626e5a98addd082

Update README.md

ppacocoursey committed 3 years ago
Verified
9091fa26a68e940a202e7c8fe69ed24fe0af1aa9

Update README.md

ppacocoursey committed 3 years ago
Verified
786b2e7c57d80b1826dfbd2571de51f3f5b315b8

Create README.md

ppacocoursey committed 3 years ago

README

The README file for this repository.

state

merge ideas from zustand + swr + valtio into a simple shared state

  • zustand: create a store (not tied to component)
  • swr: only re-render for the values you rely on (getter)
  • valtio: use proxy and broadcast
const useStore = create({
  name: 'John',
  username: 'johndoe'
})

function NameDisplay() {
  const state = useStore()
  
  // Only re-renders when state.name changes
  // mutate the value directly rather than setState
  return <input value={state.name} onChange={e => state.name = e.target.value} />
}

setState

similar version of this API in which you lose the setter but gain destructuring:

const [{ name }, setState] = useStore()

return <input value={name} onChange={e => setState({ name: e.target.value })} />

context

React Context version without creating a global store:

import { Provider, useContext } from 'state'

function Form() {
  return (
    <Provider value={{ name: 'John', username: 'johndoe' }}>
      <NameDisplay />
    </Provider>
  )
}

function NameDisplay() {
  const state = useContext()
  
  return <input value={state.name} onChange={e => state.name = e.target.value} />
}

wip thoughts