Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
这通常是由于React的生命周期阶段而发生的。如果您在渲染方法中添加
console.log
,那么任何会导致您的组件重新渲染的任何东西 - 从prop
或状态>状态>状态更改 - 将触发控制台日志发生。
您可以使用React的
使用
,如下所示,以确保在类似的特定值更改时仅在log时log log:This generally occurs due to React's lifecycle stages for components. If you add a
console.log
inside your render method then anything that would cause your component to re-render - which is complete normal fromprop
orstate
changes - will trigger the console log to occur.You can make use of React's
useEffect
as follows to make sure your console log only logs when a specific value changes like this:这很有可能是由于React的严格模式 。此模式调用了许多方法,例如功能组件主体,两次,以发现使组件不同意的副作用。如果这就是为什么看到输出两次的原因,则可以:
< strictmode>
标签来禁用严格模式。在next.js中,将reactrictMode
设置为next.config.js
文件中的false,或This is problably due to React's Strict Mode. This mode invokes many methods, like the function component body, twice in order to spot side-effects that make your components not idempotent. If this is why you see the output twice, you can:
<StrictMode>
tag. In next.js, setreactStrictMode
to false in thenext.config.js
file, or