5

I have seen online plenty of examples and tests using React and typescript where they issue something like this:

import * as ReactDOM from 'react-dom';

ReactDOM.render(
 <h1>Hello world!!</h1>, //or any other valid html snippet
 document.getElementById('root') as HTMLElement
);

However, when I try to reproduce those multiple examples in my machine, I get, first a highlight error from VS code, and then when I try to bundle I get this error:

TS2686: 'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.

If instead of putting the HTML as argument of the function I write a my SimpleComponent.render(), the bundle will be produced without errors.

What is wrong with that code snippet?

1 Answer 1

4

Try to add import * as React from 'react'. <h1> is React component (React.createClass('h1') after transpile JSX to JavaScript), but you have imported only ReactDOM.

Sign up to request clarification or add additional context in comments.

5 Comments

...because once the JSX is transpiled, that <h1>Hello world!!</h1> becomes React.createElement("h1", /*...*/);.
Great! that worked. Do you know the reason why needs that reference if the html is hardcoded there? It does not make sense to me.
You no need to reference it. This still works but as HTMLElement is wrong. This is valid ReactDOM.render( <h1>Hello world!!</h1>, document.getElementById('root') );
@HemadriDasari - The OP is using TypeScript. The as HTMLElement is correct.
Ok sorry I don’t no typescript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.