我对以下组件的输出感到非常困惑:
import { StrictMode } from "react"
import ReactDOM from "react-dom"
function Test(): React.ReactElement {
console.log('render')
Promise.resolve()
.then(() => console.log('then ' + Math.random()))
return <></>
}
ReactDOM.render(
<StrictMode>
<Test />
</StrictMode>,
document.getElementById("root")
)
Run Code Online (Sandbox Code Playgroud)
它至少在 Chrome 和 Firefox 中产生以下输出:
00:46:30.264 render
00:46:30.267 then 0.5430663800781927
00:46:30.267 then 0.9667426372511254
Run Code Online (Sandbox Code Playgroud)
我宁愿期望看到相同数量的消息。我错过了什么?
重现:https : //codesandbox.io/s/elegant-frost-dmcsl
编辑:我知道严格模式会导致额外的渲染,但如上所述,我希望消息数量相同。
编辑 2:下面的两个答案都很棒。我想在这里引用@user56reinstatemonica8 的评论:
相关:关于控制台静音的社区反馈
我正在寻找仅在某些情况下序列化瞬态信息的可能性:
@JsonInclude(Include.NON_NULL)
@Entity
public class User {
public static interface AdminView {}
... id, email and others ...
@Transient
private transient Details details;
@JsonIgnore // Goal: ignore all the time, except next line
@JsonView(AdminView.class) // Goal: don't ignore in AdminView
public Details getDetails() {
if (details == null) {
details = ... compute Details ...
}
return details;
}
}
public class UserDetailsAction {
private static final ObjectWriter writer = new ObjectMapper();
private static final ObjectWriter writerAdmin = writer
.writerWithView(User.AdminView.class); …Run Code Online (Sandbox Code Playgroud)