【问题标题】:What does the second parameter in ReactDOM.render mean?ReactDOM.render 中的第二个参数是什么意思?
【发布时间】:2020-01-23 09:59:26
【问题描述】:

我刚开始学习 React,在 ReactDOM.render 中有document.getElementById('someid'),它有什么作用?

【问题讨论】:

  • 它为你的 React 应用程序指定了一个容器在里面渲染,通常值得看看 documentation 或官方来源了解具体参数的含义。
  • 例如,如果你有 App 组件,它会在 DOM 中注入 App。

标签: reactjs


【解决方案1】:

它是第一个参数的内容将被渲染到的容器。

如果您使用 create-react-app,请在您的项目中查看 public/index.html。您应该能够找到 ID 为 root(create-react-app 的默认值)的 <div>。您可以更改该 div 的 ID,但您必须更改 index.js 中 getElementById 调用中的 ID。

这是文档所说的:https://reactjs.org/docs/react-dom.html#render

ReactDOM.render(element, container[, callback])

在提供的容器中将 React 元素渲染到 DOM 中,然后 返回对组件的引用(或为无状态返回 null 组件)。

如果 React 元素之前被渲染到容器中,这将 对其执行更新并仅根据需要更改 DOM 反映最新的 React 元素。

【讨论】:

    猜你喜欢
    • 2011-10-22
    • 2016-10-05
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    相关资源
    最近更新 更多