Skip to content
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

Collection的onBatchResize会闭包state #75

Open
yuningjiang123 opened this issue Mar 22, 2022 · 2 comments
Open

Collection的onBatchResize会闭包state #75

yuningjiang123 opened this issue Mar 22, 2022 · 2 comments

Comments

@yuningjiang123
Copy link

可在repo内example/collection.tsx文件的onBatchResize函数内添加console.log({ size1 }),观看demo效果。当多次发生resize时,打印的size1始终是第一次渲染时的size1。

原因应该是src/SingleObserver/index.tsx内,onInternalResize用useCallback做了持久化,如果onCollectionResize发生了更新则无法通知onInternalResize做出更新。因此应该加一个

const onCollectionResizeRef = React.useRef(onCollectionResize); onCollectionResizeRef.current = onCollectionResize;

之类的改造;

或者src/Collection.tsx对onBatchResize用ref包装,onResize依赖传空数组做成持久化。

@kawayidaisi
Copy link

要打印变化后的size1,自己给size1包层ref就行了
类似这样:
const ref4size1 = useRef(size1);
ref4size1.current = size1;

@yuningjiang123
Copy link
Author

从使用上来讲 我觉得如果组件内部能解决这个问题会更好一点

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants