React 与 ReactDOM? [英] React vs ReactDOM?

查看:27
本文介绍了React 与 ReactDOM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对反应有点陌生.我看到我们必须导入两件东西才能开始,ReactReactDOM,谁能解释一下区别.我正在阅读 React 文档,但它没有说.

I'm a bit new to react. I see we have to import two things to get started, React and ReactDOM, can anyone explain the difference. I'm reading through the React documentation, but it doesn't say.

推荐答案

React 和 ReactDOM 最近才被拆分为两个不同的库.在 v0.14 之前,所有 ReactDOM 功能都是 React 的一部分.这可能会引起混淆,因为任何稍微过时的文档都不会提及 React/ReactDOM 的区别.

React and ReactDOM were only recently split into two different libraries. Prior to v0.14, all ReactDOM functionality was part of React. This may be a source of confusion, since any slightly dated documentation won't mention the React / ReactDOM distinction.

顾名思义,ReactDOM 是 React 和 DOM 之间的粘合剂.通常,您只会将它用于一件事:使用 ReactDOM.render() 挂载.ReactDOM 的另一个有用功能是 ReactDOM.findDOMNode(),您可以使用它来直接访问 DOM 元素.(您应该在 React 应用程序中谨慎使用,但它可能是必要的.)如果您的应用程序是同构的",您还可以在后端代码中使用 ReactDOM.renderToString().

As the name implies, ReactDOM is the glue between React and the DOM. Often, you will only use it for one single thing: mounting with ReactDOM.render(). Another useful feature of ReactDOM is ReactDOM.findDOMNode() which you can use to gain direct access to a DOM element. (Something you should use sparingly in React apps, but it can be necessary.) If your app is "isomorphic", you would also use ReactDOM.renderToString() in your back-end code.

除此之外,还有 React.您使用 React 来定义和创建您的元素,用于生命周期挂钩等.即 React 应用程序的核心.

For everything else, there's React. You use React to define and create your elements, for lifecycle hooks, etc. i.e. the guts of a React application.

React 和 ReactDOM 被分成两个库的原因是 React Native 的到来.React 包含在 Web 和移动应用程序中使用的功能.ReactDOM 功能仅在 Web 应用程序中使用.[更新:经过进一步研究,很明显我对 React Native 的无知正在表现出来.现在,拥有 Web 和移动设备通用的 React 包似乎更像是一种愿望而不是现实.React Native 目前是一个完全不同的包.]

The reason React and ReactDOM were split into two libraries was due to the arrival of React Native. React contains functionality utilised in web and mobile apps. ReactDOM functionality is utilised only in web apps. [UPDATE: Upon further research, it's clear my ignorance of React Native is showing. Having the React package common to both web and mobile appears to be more of an aspiration than a reality right now. React Native is at present an entirely different package.]

请参阅宣布 v0.14 版本的博客文章:https://facebook.github.io/react/blog/2015/10/07/react-v0.14.html

See the blog post announcing the v0.14 release: https://facebook.github.io/react/blog/2015/10/07/react-v0.14.html

这篇关于React 与 ReactDOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆