How to start a method immediately after the state react?

Good day! Please tell me what are the options in this situation?

It the state to start another method
Now there are so
state = {
 value: this.props.bool ? this.props.value1 : this.props.value2,
 };

The situation is this ... you need based upon what props came to write the value in state, but suddenly the option should immediately start another method.
In componentDidMount state cannot be invoked as mater ELINT.
Who that can advise in this situation?
Thank you!
June 10th 19 at 15:12
2 answers
June 10th 19 at 15:14
I think it will help ComponentWillReciveProps
June 10th 19 at 15:16
import React { Component } from 'react'

export default class Example extends Component {
 constructor (props) {
super(props)

 this.state = {
 value: this.props.bool ? this.props.value1 : this.props.value2
}
}

 componentWillReceiveProps (nextProps) {
 // there are available props delayesh check with state and what you need
 if (nextProps.bool) {
 this.setState({...this.state, value: nextProps.value1})
 } else {
 this.setState({...this.state, value: nextProps.value2}, () => {
 // here you can do something else
})
}
}

 render () {
 return (
the <div>...</div>
)
}
}
state can iniinom ask. constructor in most cases react is not needed.
export default class Example extends Component {
 state = {
 value: this.props.bool ? this.props.value1 : this.props.value2,
};
}
- Lenny.Botsfo commented on June 10th 19 at 15:19
Can!, I skopipastil your component and drank from it once - Alfred.Padberg86 commented on June 10th 19 at 15:22
if there bendite functions, so too it is better not to do. Functions transmitted to other places, as well iniinom better to write:
export default class Example extends Component {
 handleClick = e => {
 // do something
};
}
- Lenny.Botsfo commented on June 10th 19 at 15:25
For the style guide to the constructor is not recommended to use...
While found such decision
state = {
 value: this.checkOpt ? this.props.value1 : this.props.value2 ,
};

 componentDidMount() {
 if (this.checkOpt) {
input.onChange(this.props.options);
}
 }


What do you say? - eulah.Hermiston commented on June 10th 19 at 15:28
Thanks didn't know about it - Alfred.Padberg86 commented on June 10th 19 at 15:31

Find more questions by tags ReduxJavaScriptReact