【问题标题】:Is it possible to replace DOM elements with React?是否可以用 React 替换 DOM 元素?
【发布时间】:2015-11-05 03:05:55
【问题描述】:

我想使用 React 渲染一个 <section> 元素,但前提是满足某些条件。

问题是我不能将<section> 嵌套在<div> 中,所以除了<body> 之外的最高级别DOM 元素是<section>

我的 HTML:

<section id="my_element"></section>

我的反应:

ReactDOM.render(
  <div className="container">
    ...
  </div>,
  document.getElementById('my_element')
);

我知道使用 Angular 指令,您可以使用 replace: true 将原始 DOM 元素替换为您的渲染元素

app.directive('myElement', function(){
  return {
    replace: true,
    ...
    }
  };
});

React 中有类似的东西吗?

【问题讨论】:

标签: javascript reactjs


【解决方案1】:

目前尚未实施: https://github.com/facebook/react/issues/1311

关注本期,了解发展情况: https://github.com/facebook/react/issues/1711

有解决方法的 StackOverflow 问题:
React.render replace container instead of inserting into
解决方法包括创建一个临时 div,然后替换节点。

【讨论】:

    【解决方案2】:

    您可以使用一些案例来实现这一点。我更喜欢这样做:

    render(){
    return [<div>test</div>, <div>test2</div>]
    }
    

    这是对您问题的回答:https://github.com/facebook/react/issues/2127

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-04
      • 2011-11-05
      • 1970-01-01
      • 2018-07-09
      • 2020-09-16
      • 2018-07-09
      • 2013-06-26
      • 2018-01-05
      相关资源
      最近更新 更多