Javascript ReactDOM 应该从哪里导入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33560843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:13:39  来源:igfitidea点击:

Where should ReactDOM be imported from?

javascriptreactjsbrowserify

提问by Sergei Basharov

After upgrading to version 0.14.2, I see an error and recommendation to use ReactDOM.render()instead of React.render(), but whence do I import it?

升级到 version 后0.14.2,我看到一个错误和建议使用ReactDOM.render()而不是React.render(),但是我从哪里导入它?

When I don't import it and just running as it is, it shows it as undefined. Is it a built-in functionality or is it a 3rd party library?

当我不导入它并按原样运行时,它显示为undefined. 它是内置功能还是第三方库?

回答by saadq

With the new update, all the DOM stuff you do should be done with ReactDOMinstead of React. It's a separate module.

随着新的更新,你做的所有东西DOM应该做ReactDOM的,而不是React。这是一个单独的模块。

Do npm install react-domand then, if you're using ES6, you can do:

这样做npm install react-dom,然后,如果你使用ES6,你可以这样做:

import ReactDOM from 'react-dom';

or if you're using ES5 you can just do:

或者,如果您使用的是 ES5,则可以执行以下操作:

var ReactDOM = require('react-dom');

and then you can use ReactDOM.render(), ReactDOM.findDOMNode(), etc in your code.

然后你可以用ReactDOM.render()ReactDOM.findDOMNode()等你的代码。