Eva Cohen
Eva Cohen

Reputation: 505

Is reactDOM.render() really needed?

I've got a question. I've been developing React components in the company I work for and never really used the reactDOM.render() function. (EXCEPT INSIDE INDEX.JS:

ReactDOM.render(
<App />, document.getElementById('root'));

Why is it needed elsewhere? in order to render to the following component?

import React from 'react';

class Compo extends React.component {
render() {
  return <div />
}

export default Compo;

The way it is done is only via <Compo /> in some other component render function, so I really cannot understand why reactDOM.render() is needed.

Thanks.

Upvotes: 3

Views: 6197

Answers (3)

Ankit Verma
Ankit Verma

Reputation: 94

Well the basis behind

 ReactDOM.render(
  element,
  document.getElementById('root')
);

is that ReactDom.render compares the element and its children and updates only what is required

For example, if your element is like the following:

const element = <div>
                 <h1>
                  "sometext getting updated"
                 </h1>
                <div>

when your application creates a new element and passes it to ReactDom.render() it updates only the text field and as long has you have a single page application ReactDom.render() should be on the index.js as a single entry point.

Upvotes: 3

Chaitanya
Chaitanya

Reputation: 11

React Components contains the logic and the content to be displayed.

But it is with react-dom package methods, we can render the content into the DOM.

ReactDom.render(element,container[,callback]);
ReactDom.render(<App/>,document.getElementById('root'));

Here ReactDom.render function loads the element <App/> into the DOM in root container.

Upvotes: 1

Mehedi Abdullah
Mehedi Abdullah

Reputation: 838

I think you have not find that..You better search for

ReactDOM.render(<App />, document.querySelector("#root"));

in your main index.js or app.js. I think without having this render method it is not possible to make virtual dom.

Upvotes: 3

Related Questions