是否犹豫要在render()中修改this.state?

时间:2015-09-18 11:16:20

标签: javascript reactjs

如果组件从其他地方接收到null,我想将状态设置为其默认值。由于没有componentWillReceiveState(),我只能检查componentWillUpdate()中的更改。但是从这个功能中我不能打电话给setState()。无论如何,componentWillUpdate()在初始渲染期间不会调用它,因此它是不可靠的。实现此目的的唯一方法是直接在this.state中修改render()。我想知道这是否违反了某些规则或假设。

2 个答案:

答案 0 :(得分:1)

由于您在谈论州,当您致电static SslConfigurator sslConfig = SslConfigurator .newInstance() .securityProtocol("TLS") .keyStoreFile("/path") .keyStorePassword("password") .keyStoreType("JKS") .trustStoreFile("/path"); static SSLContext sslCtx = sslConfig.createSSLContext(); static Client client = ClientBuilder.newBuilder().sslContext(sslCtx).build(); for (i = 0; i < 10; i++) { Response response = client.target(target).path(path) .request(MediaType.APPLICATION_JSON) .post(Entity.entity(jsonRequest.toString(), MediaType.APPLICATION_JSON)); } 时(在生命周期内)或使用null / setState时,您应该能够默认为constructor

更改getInitialState很糟糕。

答案 1 :(得分:1)

我同意@oluckyman。如果您在render中操纵状态,则组件结构出现问题。

您应该能够根据其他变量有条件地输出组件的某些位(例如this.props

render: function () {
    if (!this.props.foo) return null;

    // if you have some foo then do something else
}