LoginSignup
4
3

More than 5 years have passed since last update.

Reactでprototypingする時にdocument.bodyにレンダリングすんな!って怒られた時の回避

Last updated at Posted at 2016-06-30

ReactDomで、下記のように直接document.bodyにレンダリングすると怒られる

index.js
ReactDom.render(
  <Main />, 
  document.body
)
Warning: render(): Rendering components 
directly into document.body is discouraged,
since its children are often manipulated by third-party
scripts and browser extensions. 
This may lead to subtle reconciliation issues. 
Try rendering into a container element created for your app.

サードパーティscriptやextensionがdocument.bodyに依存してる可能性あるからやめておけという警告。とても真っ当。
productionコードなら、<div id="container"></div>のようにdivを書いてそちらを指定して回避する。

が、budoなどを使ってちょこちょこっと素振りやprototypingするときに、index.htmlをいちいち作りたくない。

解決策

createElementを直接appendChildしてそれをターゲットとする。

index.js
ReactDom.render(
  <Main />,
  document.body.appendChild(document.createElement('div'))
)

繰り返しになるが、budoのようなprototyping用の想定なので、productionではやらない。おとなしく<div id="container"></div>とhtmlに書く方が良い

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3