What is the difference? The react package holds the react source for components, state, props and all the code that is react. The react-dom package as the name implies is the glue between React and the DOM. Often, you will only use it for one single thing: mounting your application to the index.
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.
ReactDOM provides the developers with an API containing the following methods and a few more. This is one of the most important methods of ReactDOM. This function is used to render a single React Component or several Components wrapped together in a Component or a div element.
Your app probably has reactdom. render() in it and you don't realize it. Those are probably standalone examples—you need it once for every app. The book probably uses it so each example can be run on its own.
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.
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.
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.
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.]
See the blog post announcing the v0.14 release: https://facebook.github.io/react/blog/2015/10/07/react-v0.14.html
From the React v0.14 Beta release announcement.
As we look at packages like
react-native
,react-art
,react-canvas
, andreact-three
, it's become clear that the beauty and essence of React has nothing to do with browsers or the DOM.To make this more clear and to make it easier to build more environments that React can render to, we're splitting the main react package into two: react and react-dom.
Fundamentally, the idea of React has nothing to do with browsers, they just happen to be one of many targets for rendering trees of components into. The ReactDOM package has allowed the developers to remove any non-essential code from the React package and move it into a more appropriate repository.
The
react
package containsReact.createElement
,React.createClass
andReact.Component
,React.PropTypes
,React.Children
, and the other helpers related to elements and component classes. We think of these as the isomorphic or universal helpers that you need to build components.The
react-dom
package containsReactDOM.render
,ReactDOM.unmountComponentAtNode
, andReactDOM.findDOMNode
, and inreact-dom/server
we have server-side rendering support withReactDOMServer.renderToString
andReactDOMServer.renderToStaticMarkup
.
These two paragraphs explain where the core API methods from v0.13
ended up.
Before v0.14 they were part of main ReactJs file, but as in some cases we may not need both, they separate them and it starts from version 0.14, that way if we need only one of them, our app gonna be smaller due to using only one of those:
var React = require('react'); /* importing react */
var ReactDOM = require('react-dom'); /* importing react-dom */
var MyComponent = React.createClass({
render: function() {
return <div>Hello World</div>;
}
});
ReactDOM.render(<MyComponent />, node);
React package contains: React.createElement, React.createClass, React.Component, React.PropTypes, React.Children
React-dom package contains: ReactDOM.render, ReactDOM.unmountComponentAtNode, ReactDOM.findDOMNode, and react-dom/server that's including: ReactDOMServer.renderToString and ReactDOMServer.renderToStaticMarkup.
It looks like they've separated React into react
and react-dom
packages, so you don't have to use the DOM-related part for projects where you'd like to use it in non-DOM-specific cases, like in here https://github.com/Flipboard/react-canvas
where they import
var React = require('react');
var ReactCanvas = require('react-canvas');
as you can see. Without react-dom
.
To be more concise, react is for the components and react-dom is for rendering the components in the DOM. 'react-dom' acts as a glue between components and DOM. You will be using render() method of the react-dom to render components in the DOM and that's all you have to know when you are starting off with it.
The ReactDOM module exposes DOM-specific methods, while React has the core tools intended to be shared by React on different platforms (e.g. React Native).
http://facebook.github.io/react/docs/tutorial.html
React: React is a javascript library, designed for building better user interfaces.
React-DOM: React-DOM is a complimentary library to React which glues React to the browser DOM
We’re using React and whenever we use methods like render() or findDOMNode() we’re using React-DOM.
As we look at packages like react-native, react-art, react-canvas, and react-three, it's become clear that the beauty and essence of React has nothing to do with browsers or the DOM. To make this more clear and to make it easier to build more environments that React can render to, they splitting the main react package into two: react and react-dom.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With