【问题标题】:React can't replace server-side rendered node in browser callbackReact 无法替换浏览器回调中的服务器端渲染节点
【发布时间】:2016-01-04 13:26:07
【问题描述】:

我正在为一个小项目使用 React 样板:https://github.com/koistya/react-static-boilerplate。这个样板呈现了大多数 HTML 服务器端,但我想向客户端添加一些动态部分。

只要我渲染以下组件,它就会在 Chrome 和 Safari 上中断:

import React, { Component } from 'react';

class DynamicSomething extends Component {

  constructor(props, context) {
    super(props, context);
    this.state = {
      someValue: "Static"
    };
  }

  componentWillMount() {
    setTimeout(() => {this.setState({someValue: "Dynamic"});}, 1);
  }

  render() {
    return <span>{this.state.someValue}</span>;
  }
}

我收到以下错误,但在 Firefox 中它按预期工作:

Uncaught TypeError: Failed to execute 'replaceChild' on 'Node': parameter 1 is not of type 'Node'.
(program):36 FIREBASE WARNING: Exception was thrown by user callback. TypeError: Failed to execute 'replaceChild' on 'Node': parameter 1 is not of type 'Node'.
    at TypeError (native)
    at Object.Danger.dangerouslyReplaceNodeWithMarkup (eval at <anonymous> (http://localhost:3000/app.js?1451913362157:2808:3), <anonymous>:140:25)
    at Object.ReactDOMIDOperations.dangerouslyReplaceNodeWithMarkupByID (eval at <anonymous> (http://localhost:3000/app.js?1451913362157:1239:3), <anonymous>:71:27)
    at Object.wrapper [as replaceNodeWithMarkupByID] (eval at <anonymous> (http://localhost:3000/app.js?1451913362157:717:3), <anonymous>:66:21)
    at ReactCompositeComponentMixin._replaceNodeWithMarkupByID (eval at <anonymous> (http://localhost:3000/app.js?1451913362157:2862:3), <anonymous>:579:31)
    at ReactCompositeComponentMixin._updateRenderedComponent (eval at <anonymous> (http://localhost:3000/app.js?1451913362157:2862:3), <anonymous>:571:12)
    at ReactCompositeComponentMixin._performComponentUpdate (eval at <anonymous> (http://localhost:3000/app.js?1451913362157:2862:3), <anonymous>:544:10)
    at ReactCompositeComponentMixin.updateComponent (eval at <anonymous> (http://localhost:3000/app.js?1451913362157:2862:3), <anonymous>:473:12)
    at wrapper [as updateComponent] (eval at <anonymous> (http://localhost:3000/app.js?1451913362157:717:3), <anonymous>:66:21)
    at ReactCompositeComponentMixin.performUpdateIfNecessary (eval at <anonymous> (http://localhost:3000/app.js?1451913362157:2862:3), <anonymous>:421:12) 

当我直接调用 setState 而不使用 setTimeout 或者在我的实际情况下是来自 Firebase 调用的回调函数时,它确实有效。

它在 Firefox 中始终有效。

我正在使用"react": "^0.14.0", 和"babel": "^5.8.29",。

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    您不应该在componentWillMount 中异步设置状态,而是使用componentDidMount 代替。实际上,在安装组件之前,您根本不应该调用 setState(因此,即使在异步回调中,在 componentWillMount 中调用也是有风险的)。

    https://facebook.github.io/react/docs/component-specs.html#mounting-componentdidmount

    如果您想与其他 JavaScript 框架集成,使用 setTimeout 或 setInterval 设置计时器,或发送 AJAX 请求,请在此方法中执行这些操作。

    您可以在此处阅读更深入的原因:https://stackoverflow.com/a/29995104/414062

    【讨论】:

    • 太棒了,成功了!还解释了为什么我的第一个回调通常有效(设置 websocket 连接),但第二个回调没有(websocket 已经设置,所以回复快得多)。
    • 我因为这里的 cmets 而措手不及:facebook.github.io/react/docs/… 提到在componentWillMount 中调用setState 并且render 将获得新的状态。当然这不适用于异步回调。
    • 是的,一开始我也被它吓到了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 2020-06-29
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    相关资源
    最近更新 更多