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
lazy导致的两次渲染 #28
Comments
你在run函数加一个log 函数看看 渲染触发原因,然后再报告给我看下log 记录 run( {}, {
middlewares:[(stateInfo, next)=>{
console.warn(stateInfo);
next();
}],
}); |
对了,最新的react提供了 React.StrictMode 组件来包装根组件,然后触发双调用来故意触发两次渲染,以便排除一些渲染过程中书写了副作用代码导致的bug,该特性仅在开发模式有效,生产模式会关闭,更多信息可参考: http://react.html.cn/docs/strict-mode.html 你可以写个普通组件测试一下,就明白原因了 btw,提几个优化点😀 1 源头处也支持触发lazy,这样就可以不用再reducer里写lazy了 changeTab: (key: string) => ctx.mr.changeTab(key),
// 可改为
changeTab: (key: string) => ctx.mr.changeTab(key, {lazy:true}), 2 可以写个setState,替代无逻辑的reducer函数 export function changeActiveTabKey(key: string) {
return {
activeTabKey: key,
};
}
export function setModalVisible(visible: boolean) {
return {
modalVisible: visible,
};
}
// 可改为
export function setState(partialState){
return partialState;
} |
@fantasticsoul ,hi,你的建议很棒,非常感谢。。 不过上面说的问题,还是没有排查出来原因。。。你方便代码clone下来,跑一下么?我是把公司的那个项目给最小化精简了,然后用了umi3的脚手架,启动是没有问题的 |
我测了下,umi没有使用 export default React.memo(() => {
console.log('2222222222');
// your code
}); |
@fantasticsoul ,没有明白react组件自上而下株连式的渲染。。能详细说一下么?或者给个资料也行。。。 |
React的渲染时自顶向下的 <A><B><C/></B></A> 如果A触发渲染了,没有优化机制的话默认会把他所有的孩子跟着一起渲染,所以才让你包一个memo |
这里有一个bug,github地址是:https://github.com/henryzp/concent-test-demo
我的疑问是tab切换时,为什么
tabContainer
组件会render两次。。触发的是
changTab
期望的结果是render一次。。。
The text was updated successfully, but these errors were encountered: