Comments (2)
- 方法1,使用顶层api
getState(moduleName?:string)
获得
import { getState } from 'concent';
export function someReducerFn(){
const barState = getState('bar');//获得bar模块状态
}
- 方法2,从
actionCtx
里直接获取, 推荐此方法
可留意type文档,IActionCtxBase
描述如下
export interface IActionCtxBase {
callerModule: string;
module: PropKey;
committedStateMap: IAnyObj,
committedState: IAnyObj,
invoke: typeof refCtxInvoke;
lazyInvoke: typeof refCtxInvoke;
dispatch: typeof refCtxDispatch;
lazyDispatch: typeof refCtxDispatch;
rootState: IAnyObj;
globalState: IAnyObj;
moduleState: IAnyObj;
moduleComputed: IAnyObj;
setState: (obj: IAnyObj) => Promise<IAnyObj>;
refCtx: IAnyObj;
}
从描述可看出rootState
就是整个根状态,所以你在任意模块reducer方法的actionCtx
里即可取到
import { getState } from 'concent';
export function someReducerFn(payload, moduleState, actionCtx){
const barState = actionCtx.rootState.bar;//获得bar模块状态
}
bay the way,如果想在foo模块的reducer方法里调用bar模块的reducer方法去改变bar模块的状态有3种方法
- 方法1,利用dispatch字符串匹配模式
// code in models/foo/reducer.js
// 同步调用
export async function someReducerFn(payload, moduleState, actionCtx){
const ret = await actionCtx.dispatch('bar/someMethod', {val:'this is payload'});
}
// 异步调用
export function someReducerFn(payload, moduleState, actionCtx){
actionCtx.dispatch('bar/someMethod', {val:'this is payload'});
}
- 方法2,直接将对应方法引入过来
// code in models/foo/reducer.js
import * as barRd from 'models/bar/reducer';
// 同步调用
export async function someReducerFn(payload, moduleState, actionCtx){
const ret = await actionCtx.dispatch(barRd.someMethod, {val:'this is payload'});
}
// 异步调用
export function someReducerFn(payload, moduleState, actionCtx){
actionCtx.dispatch(barRd.someMethod, {val:'this is payload'});
}
- 方法3,使用顶层
reducer
对象发起调用,run接口启动concent后,所有reducer方法都会收集到一个根Reducer对象上
// code in models/foo/reducer.js
import { reducer } from 'concent';
// 同步调用
export async function someReducerFn(payload, moduleState, actionCtx){
const ret = await reducer.bar.someMethod({val:'this is payload');
}
// 异步调用
export function someReducerFn(payload, moduleState, actionCtx){
reducer.bar.someMethod({val:'this is payload');
}
此种方法不推荐,因为会失去追踪源头调用处的能力(从哪个ui实例发起的调用),当然如果用户不在乎复盘源头调用处那么就无所谓了
此方法更适合在开发模式下,打开console,输入cc.reducer.***
即可调用所有模块的各个reducer方法快速验证某些逻辑。
from helux.
已解决
from helux.
Related Issues (20)
- [RFC] 提供启用/禁用派出计算的API` enableMutates(true/false)` HOT 4
- TypeError: Cannot destructure property 'STATE_SHARED_KEY_MAP' of 't' as it is undefined. HOT 1
- [BUG] 对share对象的一部分进行 useAtom 访问时,出现意外行为 HOT 2
- [Feature] lifecycle HOT 2
- [Optimize] merge triggered non-first-level watch if they have same fnKey HOT 1
- derive 提示用户依赖需提前声明,否则会照成依赖丢失 HOT 2
- derive 派生函数如何捕获状态中可选链的依赖问题(How does the derived function capture the dependency problem of optional chaining in the state?) HOT 1
- [FEATURE] 开发一个跟踪调试插件 HOT 1
- 是否能提供 Perf Benchmark 测试报告? HOT 1
- [❤️ Sponsor] 小小鼓励,给予开发者更多动力
- [Feature] useLocalAtom
- [RoadMap] helux-2024 roadmap
- [FEATURE] 为`runMutateTask`提供入参`extraArgs`来传递给`mutate`的`task` HOT 1
- 无法控制布尔Boolean的展示 HOT 1
- [question] stopArrDep 具体行为 HOT 2
- sharex文档页访问404
- share 通过ctx调用setState 操作数组新增删除操作数组数据依赖收集无效 HOT 3
- 是否可以添加销毁mutate功能 HOT 3
- [RFC] 为响应式对象更新引入严格模式 HOT 4
- [提示] 关于死循环检测 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from helux.