Code Monkey home page Code Monkey logo

stook's Introduction

Stook

npm Coverage Status Minzipped size code style: prettier

A minimalist design state management library for React.

Documentation

The documentation site of stook is hosted at https://stook.vercel.app.

Quick start

simplest

import React from 'react'
import { useStore } from 'stook'

function Counter() {
  const [count, setCount] = useStore('Counter', 0)
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  )
}

share state

import React from 'react'
import { useStore } from 'stook'

function Counter() {
  const [count, setCount] = useStore('Counter', 0)
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  )
}

function Display() {
  const [count] = useStore('Counter')
  return <p>{count}</p>
}

function App() {
  return (
    <div>
      <Counter />
      <Display />
    </div>
  )
}

License

MIT License

stook's People

Contributors

0xkee avatar leen-c-20230331 avatar

Stargazers

此夏、若空。 avatar  avatar Jason Pang avatar  avatar viko16 avatar Roman avatar Bunny miao avatar Feiox avatar Ray avatar O/I avatar Alex Worrad-Andrews avatar Subhan Bakh avatar Nikita avatar frankfanslc avatar 拳头巴掌 avatar  avatar ziyang He avatar liangyuanzu avatar zsj1029 avatar 小灰羊 avatar  avatar Del avatar Japple avatar exia avatar  avatar JohnnyJoestar avatar Chenz avatar Andy avatar  avatar Jimmy Ye avatar  avatar kenisad5566 avatar ThomasTrainset avatar  avatar acvv_khalil avatar  avatar  avatar 明明 avatar 纯情小牛 avatar Herbert He avatar 曾浩 avatar Zhumei Song avatar unikz avatar pei.shaofeng avatar PeaceShi avatar whyming avatar geekris1 avatar  avatar  avatar budded avatar lee avatar 50岁的星空 avatar M avatar  avatar qiphon avatar  avatar 渔网的收获 avatar Daniel avatar Arvin Xu avatar buleak avatar  avatar  avatar bestkayle avatar  avatar  avatar 智轩 avatar Cong avatar 王树贤 avatar tan661 avatar cz avatar 静静是小花 avatar Michealmiker avatar Hanson Leung avatar xiongwei avatar JX Wang avatar iou90 avatar  avatar Kcat avatar xQuotes avatar  chrisma2012 avatar Ken Huang avatar  avatar Johnson avatar NoBey avatar xkyii avatar Shanoa Ice avatar fxioi avatar 丑牛 avatar  avatar  avatar  avatar

Watchers

James Cloos avatar  avatar  avatar 小灰羊 avatar Michealmiker avatar

stook's Issues

针对 useStore,能否加一个泛型来约束 key 值

export const StoreKey = {
  a: 'a',
  b: 'b',
  c: 'c',
};

export type StoreKeyType = keyof typeof StoreKey;

使用的时候就可以提示可选的 store 值

useStore<StoreKeyType>('x', 0) // 这样就会报错,因为 x 不在 StoreKey 中

这样一方面能够有提示,另一方对大型项目可以作为相应的约束参照

useStore能否支持useSelector

目前的useStore只要组件订阅了key, setState之后就会将发布订阅队列全部执行渲染,即使组件中没有用到store中的某些数据,能否实现类似redux的useSelector式API呢,另外还有一个问题就是目前这种方案,useStore中的数据初始化是在第二个参数中,两个组件订阅相同的key,初始化的数据将会以第一个先订阅为准,这样没约束规范吧,是否需要加一个store初始化的步骤呢

useFetch 与useEffect 是否不同

我使用 useFetch 获取页面数据,在我首次进入这个页面的时候,useFetch 是工作的,但是当我第二次进入的时候,useFetch 不工作了,但是useEffect 还会继续工作,请问这两者更新有什么不同呢?

初始值依赖组件的渲染顺序??

import * as React from "react";
import { render } from "react-dom";
import { useStore } from "stook";

function Display() {
  const [count] = useStore("Counter", 100);
  return <p>{count}</p>;
}


function Counter() {
  const [count, setCount] = useStore("Counter", 8);
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  );
}

// 调整 Display Counter 的位置会得到不同的初始值
function App() {
  return (
    <div>
      <Display />
      <Counter />
    </div>
  );
}

const rootElement = document.getElementById("root");
render(<App />, rootElement);

这种设计方式很容易出bug吧.....

by the way ,可以了解下Concent:
https://github.com/concentjs/concent

一个渐进式&高性能的增强型状态管理方案

可以在React组件外部订阅状态更新吗?

这个项目挺好的,但是有个问题,目前我使用Redux一部分原因是为了实现React内和外部另外一个渲染程序(WebGL)的数据双向绑定的,看了下API文档,好像在外部只能读取指定状态,没法订阅更新.
或者提供个Api,直接暴露原始Redux的stroe对象.:joy:

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.