JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > React >

React error Argument type 'HTMLElement or null' not assignable to parameter type 'Element or Document

Author:JIYIK Last Updated:2025/03/15 Views:

Use non-null assertions or type assertions to resolve the React.js error "Argument of type 'HTMLElement | null' is not assignable to parameter of type 'Element | DocumentFragment'", for example const root = createRoot(rootElement!).

React HTMLElement or null' not assignable to parameter error

The following is an example that produces this error

import App from './App';

import {StrictMode} from 'react';
import {createRoot} from 'react-dom/client';

const rootElement = document.getElementById('root');

// Argument of type 'HTMLElement | null' is not
// assignable to parameter of type 'Element | DocumentFragment'.
// Type 'null' is not assignable to type 'Element | DocumentFragment'.ts(2345)
const root = createRoot(rootElement);

root.render(
  <StrictMode>
    <App />
  </StrictMode>,
);

The problem here is the return type of document.getElementById method is HTMLElement | null.

If the provided id does not exist in the DOM, the method returns null.

On the other hand, the expected parameter type of the createRoot method is Element | DocumentFragment, so the provided parameter type does not match the expected parameter type.

One way to fix the error is to use the non-null (!)assertion operator.

import App from './App';

import {StrictMode} from 'react';
import {createRoot} from 'react-dom/client';

const rootElement = document.getElementById('root');

//  non-null (!) assertion
const root = createRoot(rootElement!);

root.render(
  <StrictMode>
    <App />
  </StrictMode>,
);

The not-null (!) assertion operator removes null and undefined from a type without doing any explicit type checking.

When you use this approach, you are basically telling TypeScript that the rootElement variable will never be null or undefined. Therefore, rootElementthe type of the variable becomes % HTMLElementinstead of % HTMLElement | null.

Alternatively, we can use simple type assertions.

import App from './App';

import {StrictMode} from 'react';
import {createRoot} from 'react-dom/client';

const rootElement = document.getElementById('root');

// 使用类型断言
const root = createRoot(rootElement as Element);

root.render(
  <StrictMode>
    <App />
  </StrictMode>,
);

Type assertions are used when we have information about the type of a value that TypeScript cannot know about.

We are actually telling TypeScript that the rootElement variable stores a value of type Element and that we don’t need to worry about it.

We determined the correct type from the error message: "Argument of type 'HTMLElement | null' is not assignable to parameter of type 'Element | DocumentFragment'".

With this error message, TypeScript is telling us: the expected parameter type of the function is Element | DocumentFragment, but you called the function with an argument of type HTMLElement | null.

The types are incompatible because the parameter type may be null.

To fix this error, we must make the passed in argument type compatible with the expected argument type.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

React Tutorial - Transferring Props

Publish Date:2025/03/16 Views:185 Category:React

React transfers Props. Props are generated when components are encapsulated. Components expose some properties (Props) to the outside world to complete some functions.

React Tutorial: Props Anti-Pattern

Publish Date:2025/03/16 Views:183 Category:React

React's Props anti-pattern, using Props to generate state in getInitialState is an anti-pattern - Anti-Pattern.

React Tutorial - Props Validation

Publish Date:2025/03/16 Views:99 Category:React

Props validation is a very useful way to use components correctly. It can avoid many bugs and problems as your application becomes more and more complex. In addition, it can make your program more readable.

Why do you need to bind event handlers in React Class Components?

Publish Date:2025/03/16 Views:58 Category:React

When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code:

Solution to the error "does not contain a default export" in React

Publish Date:2025/03/16 Views:187 Category:React

When we try to use `default import` to import from a module that does not have a `default export`, we get a "does not contain a default export" error. To fix the error, make sure the module has named exports and wrap the import in curly braces, e.g.

Solve the Module not found: Can't resolve 'react-bootstrap' error

Publish Date:2025/03/16 Views:85 Category:React

To resolve the error "Module not found: Error: Can't resolve 'react-bootstrap'", make sure to install the react-bootstrap package by opening a terminal in the root directory of the project and running the command `npm install react-bootstrap bootstrap

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial