Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
65
rated 0 times [  69] [ 4]  / answers: 1 / hits: 16868  / 5 Years ago, wed, march 13, 2019, 12:00:00

When importing react and reactDOM I have always seen:



import React from 'react';
import ReactDOM from 'react-dom';


Can I name React and ReactDOM something else?


More From » reactjs

 Answers
11

React



For React, you must import React from 'react'. You need to import the default, and you need to name it React. This is because anytime you write JSX code like <MyComponent /> or <App />, this JSX code is transpiled and uses React.createElement(). So, you need to have access to the React object as it is named.



ReactDOM



For ReactDOM, however, you are probably mostly concerned with the DOM-related methods that are part of the default export, methods like render() or hydrate(). Because that's what you'll probably be using, you can name your import module anything you want. So, you could do something like this:



import FooBar from 'react-dom'
FooBar.render(someElement)


This does work, but the standard convention is to call the imported module ReactDOM, as that would make your code more understandable to anyone else looking at it.






Additional resources:




[#52430] Friday, March 8, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pranav

Total Points: 693
Total Questions: 119
Total Answers: 119

Location: Greenland
Member since Fri, Jul 31, 2020
4 Years ago
;