Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【自荐】简单易学、功能强大的react状态管理方案:Concent #14

Open
fantasticsoul opened this issue Aug 26, 2020 · 4 comments
Labels
JavaScript JavaScript领域 学习 / 实践 学习实践类项目 具有很高的学习价值 已查看 管理员已查看,正在和小伙伴商量中~ 未来 未来期刊可以考虑推荐

Comments

@fantasticsoul
Copy link

fantasticsoul commented Aug 26, 2020

项目简介

功能与特点

  • 自动注入渲染上下文
  • 依托于运行时的依赖收集(包含状态与计算)做到精确更新
  • 统一类组件和函数组件逻辑复用方式(包括生命周期函数)
  • 支持可选的组合api
  • 支持模块化开发(state、reducer、computed、watch、lifecycle)
  • 高效的renderKey机制
  • 同时支持中心话和去中心化配置模块
  • 支持动态模块配置
  • 支持模块克隆
  • 支持reducer组合调用
  • 内置事件系统
  • 支持扩展中间件与插件
  • 支持React Devtools
  • 支持热加载
  • 兼容redux生态相关库
  • 支持 nextjs ssr
  • 支持react-native
  • 友好且完善的typescript支持

快速开始

使用npm安装concent

$ npm i --save concent

简单示例

import { run } from 'concent';
import { register, useConcent } from 'concent';

run({
  counter: {// declare a moudle named 'counter'
    state: { num: 1, numBig: 100 }, // define state
  },
  // you can also put another module here.
});

@register('counter')
class DemoCls extends React.Component{
  // commit state to store and broadcast to other refs which also belong to counter module
  inc = ()=> this.setState({num: this.state.num + 1})
  render(){
    // here if read num, it means current ins render dep keys is ['num']
    return <button onClick={this.inc}>{this.state.num}</button>
  }
}

function DemoFn(){
  const { state, setState } = useConcent('counter');
  const inc = ()=> setState({num: state.num + 1});
  return <button onClick={inc}>{state.num}</button>
}

export default function App(){
  return (
    <div>
      <ClsComp />
      <FnComp />
    </div>
  );
}

完整示例

  • Move logic to reducer and define computedwatchlifecycle

try edit this demo、 👉better js demo、👉better ts demo

import { run, defWatch } from 'concent';

run({
  counter: {
    state: { num: 1, numBig: 100 },
    computed: {
      numx2: ({ num }) => num * 2, // only num changed will trigger this fn
      numx2plusBig: ({ numBig }, o, f) => f.cuVal.numx2 + numBig // reuse computed reslult
    },
    reducer: {
      initState: () => ({ num: 8, numBig: 800 }),
      add: (payload, moduleState, actionCtx) => ({ num: moduleState.num + 1 }),
      addBig: (p, m, ac) => ({ numBig: m.numBig + 100 }),
      asyncAdd: async (p, m, ac) => {
        await delay(1000);
        return { num: m.num + 1 };
      },
      addSmallAndBig: async (p, m, ac) => {
        await ac.dispatch("add"); // hate string literal? see https://codesandbox.io/s/combine-reducers-better-7u3t9
        await ac.dispatch("addBig");
      }
    },
    watch: {
      numChange: defWatch(({ num }, o) => console.log(`from ${o.num} to ${num}`), {immediate:true})
    },
    lifecycle: {
      // loaded: (dispatch) => dispatch("initState"), // when module loaded
      mounted: (dispatch) => dispatch("initState"), // when any first ins of counter module mounted will trigger this
      willUnmount: (dispatch) => dispatch("initState") // when last ins of counter module unmount will trigger this
    }
  }
});

@register("counter")
class DemoCls extends React.Component {
  render() {
    // mr is short of moduleReducer, now you call all counter module reducer fns by mr
    return <button onClick={this.ctx.mr.add}>{this.state.num}</button>;
  }
}

function DemoFn() {
  const { moduleComputed, mr } = useConcent("counter");
  return <button onClick={mr.add}>numx2plusBig: {moduleComputed.numx2plusBig}</button>;
}
@fantasticsoul fantasticsoul changed the title 【自荐】简单易学且功能强大的react状态管理:Concent 【自荐】简单易学、功能强大的react状态管理:Concent Aug 26, 2020
@fantasticsoul fantasticsoul changed the title 【自荐】简单易学、功能强大的react状态管理:Concent 【自荐】简单易学、功能强大的react状态管理方案:Concent Aug 26, 2020
@kkzhilu kkzhilu added 学习 / 实践 学习实践类项目 具有很高的学习价值 未通过 未通过推荐要求 labels Aug 26, 2020
@kkzhilu
Copy link
Member

kkzhilu commented Aug 26, 2020

暂时不考虑有明显加群之类的项目哦,如果可以改一下话,后期会考虑在专题进行收录~

@kkzhilu kkzhilu closed this as completed Aug 26, 2020
@fantasticsoul
Copy link
Author

暂时不考虑有明显加群之类的项目哦,如果可以改一下话,后期会考虑在专题进行收录~

感谢提示,已删掉二维码

@fantasticsoul
Copy link
Author

你好,官方回复呢????

@kkzhilu kkzhilu added 未来 未来期刊可以考虑推荐 JavaScript JavaScript领域 and removed 未通过 未通过推荐要求 labels Sep 2, 2020
@kkzhilu
Copy link
Member

kkzhilu commented Sep 2, 2020

我们会在合适的期刊中审核并收录,整个开源计划正在逐渐走向系统化,流程化,请再多一点耐心哦❤️

@kkzhilu kkzhilu reopened this Sep 2, 2020
@kkzhilu kkzhilu added the 已查看 管理员已查看,正在和小伙伴商量中~ label Sep 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JavaScript JavaScript领域 学习 / 实践 学习实践类项目 具有很高的学习价值 已查看 管理员已查看,正在和小伙伴商量中~ 未来 未来期刊可以考虑推荐
Projects
None yet
Development

No branches or pull requests

2 participants