You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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')classDemoClsextendsReact.Component{// commit state to store and broadcast to other refs which also belong to counter moduleinc=()=>this.setState({num: this.state.num+1})render(){// here if read num, it means current ins render dep keys is ['num']return<buttononClick={this.inc}>{this.state.num}</button>}}functionDemoFn(){const{ state, setState }=useConcent('counter');constinc=()=>setState({num: state.num+1});return<buttononClick={inc}>{state.num}</button>}exportdefaultfunctionApp(){return(<div><ClsComp/><FnComp/></div>);}
完整示例
Move logic to reducer and define computed、watch、lifecycle
import{run,defWatch}from'concent';run({counter: {state: {num: 1,numBig: 100},computed: {numx2: ({ num })=>num*2,// only num changed will trigger this fnnumx2plusBig: ({ 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)=>{awaitdelay(1000);return{num: m.num+1};},addSmallAndBig: async(p,m,ac)=>{awaitac.dispatch("add");// hate string literal? see https://codesandbox.io/s/combine-reducers-better-7u3t9awaitac.dispatch("addBig");}},watch: {numChange: defWatch(({ num },o)=>console.log(`from ${o.num} to ${num}`),{immediate:true})},lifecycle: {// loaded: (dispatch) => dispatch("initState"), // when module loadedmounted: (dispatch)=>dispatch("initState"),// when any first ins of counter module mounted will trigger thiswillUnmount: (dispatch)=>dispatch("initState")// when last ins of counter module unmount will trigger this}}});
@register("counter")classDemoClsextendsReact.Component{render(){// mr is short of moduleReducer, now you call all counter module reducer fns by mrreturn<buttononClick={this.ctx.mr.add}>{this.state.num}</button>;}}functionDemoFn(){const{ moduleComputed, mr }=useConcent("counter");return<buttononClick={mr.add}>numx2plusBig: {moduleComputed.numx2plusBig}</button>;}
The text was updated successfully, but these errors were encountered:
fantasticsoul
changed the title
【自荐】简单易学且功能强大的react状态管理:Concent
【自荐】简单易学、功能强大的react状态管理:Concent
Aug 26, 2020
fantasticsoul
changed the title
【自荐】简单易学、功能强大的react状态管理:Concent
【自荐】简单易学、功能强大的react状态管理方案:Concent
Aug 26, 2020
项目简介
语言:Javascript
分类:React/状态管理
项目地址:https://github.com/concentjs/concent
文档地址:https://concentjs.github.io/concent-doc/
项目描述:
Concent是一个上手简单、功能强大的react状态管理方案,它是flux架构的标准实现,且完美融合了可预测、渐进式和高性能3大特点为一体。
功能与特点
快速开始
使用npm安装
concent
简单示例
完整示例
reducer
and definecomputed
、watch
、lifecycle
The text was updated successfully, but these errors were encountered: