0.0.6 • Published 1 year ago

baystate v0.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

baystate

a small, simple react state-management library.

npm install baystate # TODO 待定

创建 store

import { create } from "baystate";

export const countStore = create({
  count: 0,
  user: null,
});

export const add = () => {
  countStore.set({
    count: countStore.get().count + 1,
  });
};

export const fetchUser = async () => {
  const res = await fetch("https://some.api.com/user");
  const user = await res.json();

  countStore.set({
    user,
  });
};

组件内访问 store

import { useEffect } from "react";
import { countStore, add, fetchUser } from "./store";

const Count = () => {
  const { count, user } = countStore.use();

  useEffect(() => {
    fetchUser();
  }, []);

  return (
    <div>
      <h2>
        {user.name}:{count}
      </h2>
      <button onClick={() => add()}>add</button>
    </div>
  );
};
0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago