Open marcialwushu opened 5 years ago
Updating the state after a component mount will trigger a second render() call and can lead to property/layout thrashing.
render()
This rule is aimed to forbid the use of this.setState in componentDidMount outside of functions, such as callbacks.
this.setState
componentDidMount
The following patterns are considered warnings:
var Hello = createReactClass({ componentDidMount: function() { this.setState({ name: this.props.name.toUpperCase() }); }, render: function() { return <div>Hello {this.state.name}</div>; } });
The following patterns are not considered warnings:
var Hello = createReactClass({ componentDidMount: function() { this.onMount(function callback(newName) { this.setState({ name: newName }); }); }, render: function() { return <div>Hello {this.state.name}</div>; } });
var Hello = createReactClass({ componentDidMount: function() { this.props.onMount(); }, render: function() { return <div>Hello {this.props.name}</div>; } });
... "react/no-did-mount-set-state": [<enabled>, <mode>] ...
disallow-in-func
By default this rule forbids any call to this.setState in componentDidMount outside of functions. The disallow-in-func mode makes this rule more strict by disallowing calls to this.setState even within functions.
https://github.com/airbnb/javascript/issues/684
ESLint: (28:5) Do not use setState in componentDidMount (react/no-did-mount-set-state)
Prevent usage of setState in componentDidMount (react/no-did-mount-set-state)
Updating the state after a component mount will trigger a second
render()
call and can lead to property/layout thrashing.Rule Details
This rule is aimed to forbid the use of
this.setState
incomponentDidMount
outside of functions, such as callbacks.The following patterns are considered warnings:
The following patterns are not considered warnings:
Rule Options
disallow-in-func
modeBy default this rule forbids any call to
this.setState
incomponentDidMount
outside of functions. Thedisallow-in-func
mode makes this rule more strict by disallowing calls tothis.setState
even within functions.The following patterns are considered warnings: