Prop destructuring inside of react state
The only to keep it inside of the class property is to use a getter (which will be invoked at the first render):
state = {
get animation() {
const { active } = this.props;
return active ? 1 : 0;
}
}
Or you use an IIFE to initialize the property:
state = (() => {
const { active } = this.props;
return { animation: active ? 1 : 0 };
})()
But thats actually a bit overcomplicating. Another solution would be to move the property into the constructor:
constructor(...args) {
super(...args);
const { active } = this.props;
this.state = { animation: active ? 1 : 0 };
}
But I personally would just ignore that warning here.
You can add this rule to .eslintrc.json
"rules": {
"react/destructuring-assignment": [
"error",
"always",
{
"ignoreClassFields": true
}
]
},