小编Ant*_*nov的帖子

为什么 `Promise.then` 在 React 组件中被调用两次,而不是在 console.log 中?

我对以下组件的输出感到非常困惑:

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 的评论:

相关:关于控制台静音的社区反馈

javascript promise reactjs

66
推荐指数
2
解决办法
5610
查看次数

jackson:忽略getter,但不能使用@JsonView

我正在寻找仅在某些情况下序列化瞬态信息的可能性:

@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)

java serialization json jackson json-view

8
推荐指数
1
解决办法
5512
查看次数

标签 统计

jackson ×1

java ×1

javascript ×1

json ×1

json-view ×1

promise ×1

reactjs ×1

serialization ×1