The initial state in an ES6 class component is not an object |
The initial state in an ES6 class component must be set to an object
CheckId | RCT010115 |
---|---|
TypeName | StatePropertyInitializationObjectRule |
Severity | Error |
Type | React component |
The initial state in an ES6 class component must be set to an object.
Bad practice (state set to a string)
export class Message extends React.Component {
constructor(props) {
super(props);
this.state = "Steve";
}
render() {
return <div>Hello {this.state}</div>;
}
}
constructor(props) {
super(props);
this.state = "Steve";
}
render() {
return <div>Hello {this.state}</div>;
}
}
Good practice (state set to an object)
export class Message extends React.Component {
constructor(props) {
super(props);
this.state = { name: "Steve" };
}
render() {
return <div>Hello {this.state.name}</div>;
}
}
constructor(props) {
super(props);
this.state = { name: "Steve" };
}
render() {
return <div>Hello {this.state.name}</div>;
}
}
Disclaimer: The views and opinions expressed in this documentation and in SPCAF do not necessarily reflect the opinions and recommendations of Microsoft or any member of Microsoft. SPCAF and RENCORE are registered trademarks of Rencore. All other trademarks, service marks, collective marks, copyrights, registered names, and marks used or cited by this documentation are the property of their respective owners.