最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Does ReactDOM.render wait for the document to be loaded before rendering? - Stack Overflow

programmeradmin4浏览0评论

I'm wondering if I need to wait for the document to be loaded (i.e. listen to DOMCOntentLoaded) before calling ReactDOM.render(...). It doesn't seem like other React example apps are doing so, so I'm guessing inside the implementation of ReactDOM.render they already wait?

I'm wondering if I need to wait for the document to be loaded (i.e. listen to DOMCOntentLoaded) before calling ReactDOM.render(...). It doesn't seem like other React example apps are doing so, so I'm guessing inside the implementation of ReactDOM.render they already wait?

Share Improve this question asked Dec 6, 2017 at 16:16 sir_thursdaysir_thursday 5,41913 gold badges68 silver badges121 bronze badges 2
  • Could you post an example app which doesn't wait? I usually listen for the DOMContentLoaded event and would be interested to see the alternatives. – caffeinated.tech Commented Dec 6, 2017 at 16:20
  • I believe since you explicitly pass an element to attach react apps to, there's no need to wait for anything. either the element you pass exists or it doesnt when calling render. – azium Commented Dec 6, 2017 at 16:23
Add a ment  | 

1 Answer 1

Reset to default 7

You just need to ensure that the DOM element that your react code renders into is available at the time the <script> is evaluated. You can do this by placing the script after the dom element in your index.html file.

<!-- this will work -->
<div id="react-app"></div>
<script type="text/javascript" src="./my-react-script.js"></script>

<!-- this will NOT work -->
<script type="text/javascript" src="./my-react-script.js"></script>
<div id="react-app"></div>
发布评论

评论列表(0)

  1. 暂无评论