Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
12
rated 0 times [  19] [ 7]  / answers: 1 / hits: 91833  / 9 Years ago, wed, july 22, 2015, 12:00:00

I am hearing the term mount too many times while learning ReactJS. And there seem to be lifecycle methods and errors regarding this term. What exactly does React mean by mounting?



Examples: componentDidMount() and componentWillMount()


More From » reactjs

 Answers
2

The main job of React is to figure out how to modify the DOM to match what the components want to be rendered on the screen.



React does so by mounting (adding nodes to the DOM), unmounting (removing them from the DOM), and updating (making changes to nodes already in the DOM).



How a React node is represented as a DOM node and where and when it appears in the DOM tree is managed by the top-level API. To get a better idea about what's going on, look at the most simple example possible:



// JSX version: let foo = <FooComponent />;
let foo = React.createElement(FooComponent);


So what is foo and what can you do with it? foo, at the moment, is a plain JavaScript object that looks roughly like this (simplified):



{
type: FooComponent,
props: {}
}


It's currently not anywhere on the page, i.e. it is not a DOM element, doesn't exist anywhere in the DOM tree and, aside from being React element node, has no other meaningful representation in the document. It just tells React what needs to be on the screen if this React element gets rendered. It is not mounted yet.



You can tell React to mount it into a DOM container by calling:



ReactDOM.render(foo, domContainer);


This tells React it's time to show foo on the page. React will create an instance of the FooComponent class and call its render method. Let's say it renders a <div />, in that case React will create a div DOM node for it, and insert it into the DOM container.



This process of creating instances and DOM nodes corresponding to React components, and inserting them into the DOM, is called mounting.



Note that normally you'd only call ReactDOM.render() to mount the root component(s). You don't need to manually mount the child components. Every time a parent component calls setState(), and its render method says a particular child should be rendered for the first time, React will automatically mount this child into its parent.


[#65727] Monday, July 20, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dayanaracolleeng

Total Points: 7
Total Questions: 96
Total Answers: 104

Location: Mayotte
Member since Mon, Sep 12, 2022
2 Years ago
;