MIT License

项目介绍

atlassian/react-sweet-state 是一个托管在 GitHub 上的开源项目,主要使用 JavaScript 编写,采用 MIT License 许可证,目前获得 885 个 Star、61 次 Fork。

仓库全名
atlassian/react-sweet-state
开源许可证
MIT License
Star 数
885
Fork 数
61
未关闭 Issue
30

README 摘要

react-sweet-state The good parts of Redux and React Context in a flexible, scalable and easy to use state management solution Philosophy sweet-state is heavily inspired by Redux mixed with Context API concepts. It has render-prop components or hooks, connected to Store instances (defined as actions and initial state), receiving the Store state (or part of it) and the actions as a result. Each Hook, or Subscriber, is…

完整内容见 GitHub 原始 README 或本页 。

常见问题

react-sweet-state 用什么语言开发?

主要使用 JavaScript 编写,采用 MIT License 许可证,目前有 885 个 Star。

react-sweet-state 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 react-sweet-state 的源码?

可以直接克隆仓库:git clone https://github.com/atlassian/react-sweet-state.git;安装与运行步骤见仓库 README。

react-sweet-state 最近还在维护吗?

仓库最近一次更新为 2月14日,当前有 30 个未关闭 Issue;Star 数为 885,可作为社区活跃度的参考。

关于

GitHub 原始描述(英文)

Shared state management solution for React

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
1892
价值分
636
活跃状态
活跃

README

  ⚠️ Warning ⚠️
  This package is deprecated and the last version has been tested only up to React 18.

react-sweet-state logo

react-sweet-state

The good parts of Redux and React Context in a flexible, scalable and easy to use state management solution

Philosophy

sweet-state is heavily inspired by Redux mixed with Context API concepts. It has render-prop components or hooks, connected to Store instances (defined as actions and initial state), receiving the Store state (or part of it) and the actions as a result.

Each Hook, or Subscriber, is responsible to get the instantiated Store (creating a new one with initialState if necessary), allowing sharing state across your project extremely easy.

Similar to Redux thunks, actions receive a set of arguments to get and mutate the state. The default setState implementation is similar to React setState, accepting an object that will be shallow merged with the current state. However, you are free to replace the built-in setState logic with a custom mutator implementation, like immer for instance.

Basic usage

npm i react-sweet-state
# or
yarn add react-sweet-state
Creating and consuming stores
import { createStore, createHook } from 'react-sweet-state';

const Store = createStore({
  // value of the store on initialisation
  initialState: {
    count: 0,
  },
  // actions that trigger store mutation
  actions: {
    increment:
      () =>
      ({ setState, getState }) => {
        // mutate state synchronously
        setState({
          count: getState().count + 1,
        });
      },
  },
  // optional, unique, mostly used for easy debugging
  name: 'counter',
});

const useCounter = createHook(Store);
// app.js
import { useCounter } from './components/counter';

const CounterApp = () => {
  const [state, actions] = useCounter();
  return (
    <div>
      <h1>My counter</h1>
      {state.count}
      <button onClick={actions.increment}>+</button>
    </div>
  );
};

Documentation

Check the docs website or the docs folder.

Examples

See sweet-state in action: run npm run start and then go and check each folder:

  • Basic example with Flow typing http://localhost:8080/basic-flow/
  • Advanced async example with Flow typing http://localhost:8080/advanced-flow/
  • Advanced scoped example with Flow typing http://localhost:8080/advanced-scoped-flow/

Contributing

To test your changes you can run the examples (with npm run start). Also, make sure you run npm run preversion before creating you PR so you will double check that linting, types and tests are fine.

Thanks

This library merges ideas from redux, react-redux, redux-thunk, react-copy-write, unstated, bey, react-apollo just to name a few. Moreover it has been the result of months of discussions with ferborva, pksjce, TimeRaider, dpisani, JedWatson, and other devs at Atlassian.


With ❤️ from Atlassian

评论

暂无评论