Skip to content

Use react-hooks ad observer, use immer create immertable global state

Notifications You must be signed in to change notification settings

ymzuiku/react-ob

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-ob

Only use observer do react state, only 0.4kb in gzip.

start

  1. Add ObProvider in App.tsx
<ObProvider>
  <App />
</ObProvider>
  1. Use useChannel and Ob in Page
function Page() {
  const value = useChannel(0);

  return (
    <div>
      <Ob channels={[value]}>{(value) => <div>num: {value}</div>}</Ob>
      <button onClick={() => value.set(value() + 1)}></button>
    </div>
  );
}
  1. Use global channel:
const name = channel("");

function UserPage() {
  return (
    <Subscribe channels={[name]}>
      {(_name) => <div>User name: {_name}</div>}
    </Subscribe>
  );
}

function UserSettingPage() {
  return (
    <div>
      <div> setting username:</div>
      <input onChange={(e) => name.set(e.target.value)} />
    </div>
  );
}
  1. Use localStorage channel:
const name = channelWithStorage("user-name", "");

function UserPage() {
  return (
    <Subscribe channels={[name]}>
      {(_name) => <div>User name: {_name}</div>}
    </Subscribe>
  );
}

function UserSettingPage() {
  return (
    <div>
      <div> setting username:</div>
      <input onChange={(e) => name.set(e.target.value)} />
    </div>
  );
}

About

Use react-hooks ad observer, use immer create immertable global state

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published