no-render-return-value
Rule category
Restriction.
What it does
Prevents usage of the return value of ReactDOM.render
.
Why is this bad?
ReactDOM.render()
currently returns a reference to the root ReactComponent instance. However, using this return value is legacy and should be avoided because future versions of React may render components asynchronously in some cases. If you need a reference to the root ReactComponent instance, the preferred solution is to attach a callback ref to the root element.
Examples
Failing
import React from "react";
import import ReactDOM
ReactDOM from "react-dom";
const const instance: void
instance = import ReactDOM
ReactDOM.const render: ReactDOM.Renderer
(element: React.FunctionComponentElement<any> | Array<React.FunctionComponentElement<any>>, container: ReactDOM.Container | null, callback?: () => void) => void (+6 overloads)
render(<JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div React.HTMLAttributes<HTMLDivElement>.id?: string | undefined
id="app" />, var document: Document
[MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/document)document.Document.body: HTMLElement
Specifies the beginning and end of the document body.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/Document/body)body);
// - Do not depend on the return value from '{{objectName}}.render'.
Passing
import React from "react";
import import ReactDOM
ReactDOM from "react-dom";
function function doSomethingWithInst(inst: HTMLDivElement | null): void
doSomethingWithInst(inst: HTMLDivElement | null
inst: HTMLDivElement | null) {
// ...
}
import ReactDOM
ReactDOM.const render: ReactDOM.Renderer
(element: React.FunctionComponentElement<any> | Array<React.FunctionComponentElement<any>>, container: ReactDOM.Container | null, callback?: () => void) => void (+6 overloads)
render(<JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div React.HTMLAttributes<HTMLDivElement>.id?: string | undefined
id="app" React.RefAttributes<HTMLDivElement>.ref?: React.LegacyRef<HTMLDivElement> | undefined
Allows getting a ref to the component instance.
Once the component unmounts, React will set `ref.current` to `null`
(or call the ref with `null` if you passed a callback ref).ref={function doSomethingWithInst(inst: HTMLDivElement | null): void
doSomethingWithInst} />, var document: Document
[MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/document)document.Document.body: HTMLElement
Specifies the beginning and end of the document body.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/Document/body)body);