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 computed、watch、lifecycle
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 ;
}
}
项目简介
语言:Javascript
分类:React/状态管理
项目地址:https://github.com/concentjs/concent
文档地址:https://concentjs.github.io/concent-doc/
项目描述: Concent是一个上手简单、功能强大的react状态管理方案,它是flux架构的标准实现,且完美融合了可预测、渐进式和高性能3大特点为一体。
功能与特点
快速开始
使用npm安装
concent
简单示例
完整示例
reducer
and definecomputed
、watch
、lifecycle
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 ; } }
function DemoFn() { const { moduleComputed, mr } = useConcent("counter"); return ; }