[英]componentDidUpdate not firing after each render
I'm tracking when componentDidUpdate
and render
are firing with log statements.我正在跟踪
componentDidUpdate
和render
何时使用日志语句触发。
The log statements in componentDidUpdate
do not fire after render
. componentDidUpdate
的日志语句不会在render
之后触发。 I have used breakpoints to confirm this isn't a timing issue.我已经使用断点来确认这不是时间问题。
I'm using "render props" to wrap the component in question.我正在使用“渲染道具”来包装有问题的组件。 My code (stripped down) is below.
我的代码(精简)如下。 This is the output of the logging.
这是日志记录的输出。 Sometimes I'll get
componentDidUpdate
to fire, but inconsistently and it's never the final thing, a RENDER
always shows up in my logs last, never UPDATE
.有时我会触发
componentDidUpdate
,但不一致并且它永远不是最后一件事,一个RENDER
总是最后出现在我的日志中,从来没有UPDATE
。
As I understand it componentDidUpdate
should fire even if the update does not modify the DOM (though the renders here do update the DOM.) I've tried React@16.11.x
and React@16.12.x
with identical results.据我了解,即使更新没有修改 DOM,
componentDidUpdate
应该触发(尽管这里的渲染确实更新了 DOM。)我已经尝试过React@16.11.x
和React@16.12.x
,结果相同。
class MyWrapper extends React.PureComponent {
render() {
const { buttonDefinitions } = this.props;
return (
<InfoProvider
render={infoProps => {
return (
<MyMenu
{...{ buttonDefinitions, infoProps }}
/>
);
}}
/>
);
}
}
class MyMenu extends React.Component {
componentDidUpdate() {
log.warn('UPDATE');
}
render() {
log.warn('RENDER');
const { buttonDefinitions } = this.props;
return (
<MenuWrapper>
{buttonDefinitions.map(buttonDef => (
<MyButton {...buttonDef} />
))}
</MenuWrapper>
);
}
}
As per react docs , if you are using render props with React pure component, then shallow prop comparison will always return false for new props.根据react docs ,如果您将渲染道具与 React 纯组件一起使用,那么浅层道具比较将始终为新道具返回 false。 In this case, for each render it will generate a new value for the render prop.
在这种情况下,对于每个渲染,它将为渲染道具生成一个新值。 As new props getting created & not updating previous one it won't call componentDidUpdate.
随着新道具的创建和不更新之前的道具,它不会调用 componentDidUpdate。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.