Movatterモバイル変換


[0]ホーム

URL:


Using specific APIs insideReact.render() should be avoided

  • REACT_MISUSED_API_IN_RENDER
  • Error
  • Medium
  • react

This rule applies when specific APIs are used insideReact.render().

The below APIs cannot be used insideReact.render() because it triggers nested component updates or accesses unmounted components:

  1. ReactDOM.render()
  2. ReactDOM.unmountComponentAtNode()
  3. ReactDOM.findDOMNode()

In the above cases, React outputs a warning message. Therefore, these APIs should be called insidecomponentDidUpdate() orcomponentDidMount() instead ofReact.render().

For your reference, usingsetstate() insideReact.render() also triggers the nested component updates. This case is detected byREACT_BAD_UPDATE_STATE rule.

Noncompliant Code ExampleCompliant Code Example
1import React from 'react';1import React from 'react';
2import ReactDOM from 'react-dom';2import ReactDOM from 'react-dom';
33
4export class MyComponent extends React.Component {4export class MyComponent extends React.Component {
5 componentDidMount() {
6 ReactDOM.findDOMNode(this.node).focus();
7 }
5 render() {8 render() {
6 ReactDOM.findDOMNode(this.node).focus(); // REACT_MISUSED_API_IN_RENDER alarm because the component is not mounted yet.
7 return (9 return (
8 <div>10 <div>
9 <input type="text" ref={(node) => this.node = node} />11 <input type="text" ref={(node) => this.node = node} />
10 </div>12 </div>
11 );13 );
12 }14 }
13}15}

Noncompliant Code Example

View with compliant examples side by side
import React from 'react';import ReactDOM from 'react-dom';export class MyComponent extends React.Component {  render() {    ReactDOM.findDOMNode(this.node).focus(); // REACT_MISUSED_API_IN_RENDER alarm because the component is not mounted yet.    return (      <div>        <input type="text" ref={(node) => this.node = node} />      </div>    );  }}

Compliant Code Example

View with noncompliant examples side by side
import React from 'react';import ReactDOM from 'react-dom';export class MyComponent extends React.Component {  componentDidMount() {    ReactDOM.findDOMNode(this.node).focus();  }  render() {    return (      <div>        <input type="text" ref={(node) => this.node = node} />      </div>    );  }}

Version

This rule was introduced in DeepScan 1.6.0-beta.

See

  • ReactDOM APIs

  • React Warning: A component is accessing findDOMNode inside its render(). render() should be a pure function of props and state. It should never access something that requires stale data from the previous render, such as refs. Move this logic to componentDidMount and componentDidUpdate instead.

Was this documentation helpful?

Last updated on April 29, 2025

[8]ページ先頭

©2009-2025 Movatter.jp