einfach-state
TypeScript icon, indicating that this package has built-in type declarations

0.5.1 • Public • Published

npm i einfach-state

第一步 创建一个 atom实例

import { atom } from 'einfach-state'

const helloWorldAtom = atom('Hello World')
const objAtom = atom({})

第二步 在组件中调用

import { useAtom, atom } from 'einfach-state';

const countAtom = atom(0)

function Counter() {
  const [count, setCount] = useAtom(countAtom);
  return (
    <h1>
      {count}
      <button onClick={() => setCount((c) => c + 1)}>one up</button>
    </h1>
  );
}

从状态A衍生状态B

import { atom } from 'einfach-state';

const countAtom = atom(0)
const doubleCountAtom = atom((get)=>{
    return get(countAtom) * 2
})

创建多个store数据仓

import { createStore } from 'einfach-state'

const store = createStore()
const countAtom = atom(0)

function Counter() {
  const [count, setCount] = useAtom(countAtom , { store });
  return (
    <h1>
      {count}
      <button onClick={() => setCount((c) => c + 1)}>one up</button>
    </h1>
  );
}

Readme

Keywords

Package Sidebar

Install

npm i einfach-state

Weekly Downloads

35

Version

0.5.1

License

MIT

Unpacked Size

45.2 kB

Total Files

77

Last publish

Collaborators

  • allroad88888888