-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Feat/use reactive #627
Feat/use reactive #627
Conversation
温浩明 seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
hi~不要重复提交 PR,如果需要做补充修改的话,直接在原 PR 上 push 就可以了 |
呃... 我看到原来那个pr 不知道为啥远程仓库变成 unknown repository 😑 。。 |
|
||
let state = useCreation(() => { | ||
return observer(observerState, () => { | ||
setObserverState({ ...observerState }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里 {...observerState}
如果只是为了触发重渲染的话,其实可以直接用 useUpdate
既然是 reactive ,就不应该引用改变了。。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是呀 😅
|
||
import useCreation from '../useCreation'; | ||
|
||
function observer(initialVal, cb) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
所以现在是不依赖 vue reactive 了么。。直接用 Proxy 的话,不知道会不会在边界情况下不可靠?(例如数组的操作)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是呀,不依赖外面的包,直接使用proxy了,性能是最好的,至于数组的操作在改变原数组的时候能够触发,比如push pop之类,如果涉及slice肯定没效果的。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不过这个hooks 最后返回的结果是一个可变对象,不过我觉得这样可能对于react本身不可变对象概念有点违背.. 比如如果直接使用useEffect去监听对象引用就不行了,需要监听值,不过我想到一个方法就是 let [ state, immutableObj ] = useReactive({count:0 })
第一个state
是一个可变的操作对象,每次操作它, 第二个immutableObj
每次都是一个全新且完整的引用对象. 你觉得这样接口设计可以吗,而且我代码写好了 可以贴出来你看么😳
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
immutableObj怎么使用呢?
我思考过一个问题,所以翻到了这里看看有没有什么讨论。如果把 mutableState传给被React.memo的子组件,在子组件中改变它的值,那么子组件可能不会更新(因为proxy里面更新的是父组件,子组件被memo了,同时proxy的引用没有变)。
就算把ImmutableObj传递给子组件,可能会带来另一个问题,比如如果子组件用了const anotherMutable = useReactive(immutableObj)的话,然后子组件里面改变了anotherMutable,这时父组件就不会更新了,但是父组件使用的值是有变化的。
我没有想到好的解法,一个有点复杂的想法是这样的
- 维护一个对应的rootImmutableState。
- 每个proxy实例都提供一个
toImmutableState
的方法,获得rootImmutableState中与这个proxy对应的部分。 - 提供一个HOC,封装React.memo,比较的时候如果是proxy对象,就比较其
toImmutableState
的值。 - 对于useMemo,useEffect等里面的的dependency的引用,需要手动toImmutableState
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
😭 Deploy PR Preview b29b48a failed. Build logs 🤖 By surge-preview |
重构第一次提交的useReactive代码
Close #615