-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
fix(ssr): csr 和 dynamicImport 一起使用导致组件 2 次 mount #6057
Conversation
Component in wrapInitialPropsFetch should not be changed.
ci 好像挂了 |
Codecov Report
@@ Coverage Diff @@
## master #6057 +/- ##
=======================================
Coverage 82.94% 82.94%
=======================================
Files 158 158
Lines 3500 3501 +1
Branches 941 920 -21
=======================================
+ Hits 2903 2904 +1
Misses 588 588
Partials 9 9
Continue to review full report at Codecov.
|
不好意思, 已修复 |
加个用例吧。 |
expect 渲染次数的用例 |
/rebase |
试了下这样改还是会渲染两次 |
渲染是 2 次, mount 应该只有一次 |
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.
试了下,还是有问题
@Baoyx007 我这里弄了一个 demo,还是 mount 2 次 import { useEffect, useRef } from 'react';
import { Link } from 'umi';
function IndexPage(props) {
const ref = useRef(0);
useEffect(() => {
ref.current += 1;
}, []);
return (
<div>
<p>count:{ref.current}</p>
<Link to="/news">/news</Link>
</div>
);
}
IndexPage.getInitialProps = () => {
return {
a: 111,
};
};
export default IndexPage; 应该切回来 count 为 1,应该是 0 才对 |
这是 mount 一次, 渲染了 2 次吧? 第一次 props 是 {}, mount 后 current +1 . 由于渲染了两次, 所以你能看到 current 是 1. |
umi 在切换时 mount 了两次,可能有问题 https://codesandbox.io/s/amazing-dust-wh1fh?file=/pages/index.js |
这个问题解决了吗 |
我也焦急等待中 |
焦急等待中... |
我现在临时在 页面组件上 加上 React.memo 外包解决 |
这个问题还是存在 |
Checklist
npm test
passesClose #5881
Description of change
改变 Component 会导致一次强制重渲染. 而这样, 会让组件中的 didMount 被调用两次.
事实上, 只需要将新的 initialProps 传给 LoadableComponent, LoadableComponent 会将这个 props 传递下去.
这样, 第二次渲染就不是强制渲染了.