score:188
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.
score:3
the main goal of react js is to create reusable components. here, components are the individual parts of a webpage. for example, in a webpage the header is a component, the footer is a component, a toast notification is a component and etc. the term "mount" tells us that these components are loaded or rendered in the dom. these are many top-level apis and methods dealing with this.
to make it simple, mounted means the component has been loaded to the dom and unmounted means the components has been removed from the dom.
score:5
mounting refers to the initial page loading when your react component is first rendered. from react documentation for mounting: componentdidmount:
invoked once, only on the client (not on the server), immediately after the initial rendering occurs. at this point in the lifecycle, the component has a dom representation which you can access via react.finddomnode(this).
you can contrast this with componentdidupdate function, which is called everytime that react renders (except for the initial mount).
score:8
https://facebook.github.io/react/docs/tutorial.html
here, componentdidmount is a method called automatically by react when a component is rendered.
the concept is that you're telling reactjs, "please take this thing, this comment box or spinning image or whatever it is i want on the browser page, and go ahead and actually put it on the browser page. when that's done, call my function that i've bound to componentdidmount
so i can proceed."
componentwillmount
is the opposite. it will fire immediately before your component renders.
see also here https://facebook.github.io/react/docs/component-specs.html
finally, the "mount" term seems to be unique to react.js. i don't think it is a general javascript concept, or even a general browser concept.
score:12
mounting refers to the component in react (created dom nodes) being attached to some part of the document. that's it!
ignoring react you can think of these two native functions as mounting:
which are likely the most common functions react uses to mount internally.
think of:
componentwillmount === before-mount
and:
componentdidmount === after-mount
score:53
react is an isomorphic/universal framework. that means that there is a virtual representation of the ui component tree, and that is separate from the actual rendering that it outputs in the browser. from the documentation:
react is so fast because it never talks to the dom directly. react maintains a fast in-memory representation of the dom.
however, that in-memory representation is not tied directly to the dom in the browser (even though it is called virtual dom, which is an unfortunate and confusing name for an universal apps framework), and it is just a dom-like data-structure that represents all the ui components hierarchy and additional meta-data. virtual dom is just an implementation detail.
"we think the true foundations of react are simply ideas of components and elements: being able to describe what you want to render in a declarative way. these are the pieces shared by all of these different packages. the parts of react specific to certain rendering targets aren't usually what we think of when we think of react." - react js blog
so, the conclusion is that react is rendering agnostic, which means that it doesn't care about what is the final output. it can be a dom tree in the browser, it can be xml, native components or json.
"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." - react js blog
now, that you know how react works, it is easy to answer your question :)
mounting is the process of outputting the virtual representation of a component into the final ui representation (e.g. dom or native components).
in a browser that would mean outputting a react element into an actual dom element (e.g. an html div or li element) in the dom tree. in a native application that would mean outputting a react element into a native component. you can also write your own renderer and output react components into json or xml or even xaml if you have the courage.
so, mounting/unmounting handlers are critical to a react application, because you can only be sure a component is output/rendered when it is mounted. however, the componentdidmount
handler is invoked only when rendering to an actual ui representation (dom or native components) but not if you are rendering to an html string on the server using rendertostring
, which makes sense, since the component is not actually mounted until it reaches the browser and executes in it.
and, yes, mounting is also an unfortunate/confusing name, if you ask me. imho componentdidrender
and componentwillrender
would be much better names.
Source: stackoverflow.com
Related Query
- What are these three dots in React doing?
- What is the difference between React Native and React?
- What is the difference between using constructor vs getInitialState in React / React Native?
- How to access a DOM element in React? What is the equilvalent of document.getElementById() in React
- What is the best way to access redux store outside a react component?
- React native text going off my screen, refusing to wrap. What to do?
- What are React controlled components and uncontrolled components?
- React functional stateless component, PureComponent, Component; what are the differences and when should we use what?
- What does it mean when they say React is XSS protected?
- what is right way to do API call in react js?
- What is "Mounting" in React js?
- What is the best way to redirect a page using React Router?
- What is the best way to trigger onchange event in react js
- What happens when using this.setState multiple times in React component?
- What is service worker in react js
- What does ...rest mean in React JSX?
- What is the best way to deal with a fetch error in react redux?
- what is the preferred way to mutate a React state?
- What is the difference between NextJs and Create React App
- What is the quickest way to convert a React app to React Native?
- React with Redux? What about the 'context' issue?
- What does calling super() in a React constructor do?
- What will happen if I use setState() function in constructor of a Class in ReactJS or React Native?
- What does registerServiceWorker do in React JS?
- What is difference between React vs React Fiber?
- What is the TypeScript return type of a React stateless component?
- What folders should be git ignored in React Native project?
- react custom hooks vs normal functions, what is the difference
- What is the default unit of style in React Native?
- What is React component 'displayName' is used for?
More Query from same tag
- Integrate chartjs-chart-treemap with react-chartjs-2
- Select All / Unselect All option in react-select
- setInterval with setState in React
- React click on filtered List
- useEffect not printing div element react hooks
- Component loading and transition animations with react-router
- Post multiple Request to REST API django from Array using Axios in Reactjs
- Use map function for array object and display in table in React
- Load dropdownlist data from api in reactjs
- Sort method and indexes
- React js, why doesn't my component re-render when I change the value of open?
- react-testing-library - test component that uses useContext hook - context persists between tests
- How to connect login API in reactjs
- How to attach drag event handlers to a React component using TypeScript
- Editing code of react npm modules in node module folder
- How to Redirect to an external URL while sending formData with POST request via fetch() in ReactJS?
- React - Material label not in right position in Textfield
- How to prevent a prop from being passed to the extended component?
- How to trigger GET after POST in React so new item would show without reloading page
- Copy / migrate users from Firebase Auth to Google Identity Platform
- React DataGrid: how to know which rows are being rendered?
- React local element not updating?
- Serving up a single page rect app using java vert.x web server
- How to redirect to another page using navlink react router
- load options of a select box dynamically based on value of another select box in react
- How to compare array objects present in two different React states
- Passing value by using Context API and useContext (React js)
- Map array into a row of a table in react
- React/Material UI - Handling open/close boxes in child components
- return empty string if condition isn't met