Giter Club home page Giter Club logo

Comments (10)

itmanyong avatar itmanyong commented on June 3, 2024

代码不全不是很好判断,不过从你的描述可能会存在一下情况。
1.你的userLogin是定义在reducer中的,reducer函数的返回内容会融合到对应的module中去,这是reducer的默认能力。
2.上图中的state内容应该是你system模块的默认数据吧,调用userlogin之后返回的数据融合到了system模块中,所以会有上图中的moduleState的数据。
3.moduleState会自动同步到实例的state,但是上图明显没有同步,导致这个问题的原因可能是因为你的state在不应该解构的地方被提前解构出来引用,失去响应导致的。
4.关于“额外的字段扩展”,其实就是你的组件内部状态,例如你的组件state={a:111},模块state={b:222},那么你的useConcent解构出来的state最终会是{a:111,b:222}这个形式。但是a的值你是只能在当前组件实例中读取和使用,而b由于是全局模块的,其他地方也可以使用,简单地说moduleState=模块的数据,state是实例私有数据+连接的模块的数据融合过后的

from helux.

itmanyong avatar itmanyong commented on June 3, 2024

额外啰嗦一句:不要在setup函数的直接内部作用域解构状态,例如:function setup(ctx){const {state,props}=ctx;};这是不可取的。但你可以在内部函数内部这样解构。例如:function setup(ctx){ return { testFunc(){const {state}=ctx;} } };这是可行的

from helux.

pawover avatar pawover commented on June 3, 2024

额外啰嗦一句:不要在setup函数的直接内部作用域解构状态,例如:function setup(ctx){const {state,props}=ctx;};这是不可取的。但你可以在内部函数内部这样解构。例如:function setup(ctx){ return { testFunc(){const {state}=ctx;} } };这是可行的

感谢您的分析解答和关于 setup 的建议!我在代码中的其他地方并没有解构过 state,我不知道是否是其他的问题导致的,以下是可以复现的一份简单代码

https://github.com/Handpear/concent-state-test

from helux.

fantasticsoul avatar fantasticsoul commented on June 3, 2024

https://github.com/Handpear/concent-state-test

你的代码不对?还未提交吗

image

from helux.

pawover avatar pawover commented on June 3, 2024

https://github.com/Handpear/concent-state-test

你的代码不对?还未提交吗

image

奇怪,我自己打印出来的结果和你的不一致

image

版本:
node v14.17.6
pnpm 6.27.1

from helux.

fantasticsoul avatar fantasticsoul commented on June 3, 2024

你提交下最新的代码呗,不信你重新克隆现在这份代码试试

from helux.

pawover avatar pawover commented on June 3, 2024

你提交下最新的代码呗,不信你重新克隆现在这份代码试试

我在项目中添加了一个视频(时长1分06秒),演示了从 clone 到 console.log,我不知道是否是其他地方出了问题

from helux.

itmanyong avatar itmanyong commented on June 3, 2024

这个问题确实存在,异步情况下moduleState的状态未即时同步到实例state。
1.可能设计如此moduleState的实例state同步工作是异步的。那么这样就是正常的。
2.可能是由于组件未直接使用token导致的懒同步?当组件加上对token的使用后截然不同,state和moduleState是完全一致的。
22222

from helux.

fantasticsoul avatar fantasticsoul commented on June 3, 2024

sorry,发现你的代码提交的不对,和你的图示表示完全不一样,你的state仅这些定义
image

和你的截图不一样,不过我明白你的意思了,我来解释下这个问题的原因吧, ctx.state 是实例状态,ctx.moduleState 是模块状态,moduleState 是 state 的子集,如果用户不主动在视图渲染时使用到moduleState里任何值,意味着当前视图对moduleState没有任何依赖,所以concent不会主动把moduleState 更新到 state里,如果主动使用了则是在组件触发渲染时(何时渲染的调度由react控制)才会把 moduleState 合并到 state上,而你的示例里调用方法,然后直接在then里面打印 state,此时state依然是旧的,只有组件重渲染时才是新的,所以如果你在函数或者回调里使用模块状态,建议总是取 moduleState,state仅组件重渲染那一刻才会是最新的。

from helux.

fantasticsoul avatar fantasticsoul commented on June 3, 2024

这个问题确实存在,异步情况下moduleState的状态未即时同步到实例state。 1.可能设计如此moduleState的实例state同步工作是异步的。那么这样就是正常的。 2.可能是由于组件未直接使用token导致的懒同步?当组件加上对token的使用后截然不同,state和moduleState是完全一致的。 22222

ctx.state 是实例状态,ctx.moduleState 是模块状态,moduleState 是 state 的子集,如果用户不主动在视图渲染时使用到moduleState里任何值,意味着当前视图对moduleState没有任何依赖,所以concent不会主动把moduleState 更新到 state里,如果主动使用了则是在组件触发渲染时(何时渲染的调度由react控制)才会把 moduleState 合并到 state上,示例里调用方法然后直接在then里面打印 state,此时state依然是旧的,只有组件重渲染时才是新的,所以如果你在函数或者回调里使用模块状态,建议总是取 moduleState,state仅组件重渲染那一刻才会是最新的。

from helux.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.