Abhinav Gupta
Abhinav Gupta

Reputation: 33

Why can't I import React as "react"?

Problem described here too, but the response was not elaborative React can't be found

import React from 'react' <- I know this statement is correct

Since "React" is a default export and not a named export, shouldn't this statement work too:

import react from 'react'

I know React.createElement() will be called in future, but why isn't react.createElement() correct? After all, the word "React" is just a name to refer to 'react' module.

Upvotes: 1

Views: 4974

Answers (1)

DevAddict
DevAddict

Reputation: 2015

In the old versions of react-scripts which uses webpack as a bundler, you need to define a React object in your code where you use JSX because when the bundler is handling your code uses the defined React object to call for the nessecerary methods like React.createElement and everywhere else that react is needed. That is why if you remove the React import or write the name in any other fashion you will face an error

Upvotes: 1

Related Questions