I am trying to use setState to change value in components state (personId), with a value (pid) that I get from inside of render().
There is three scenarios inside of this component. All of those are kind of same scenarios, but with small changes depending on what I receive from API-call with Axios.
I change state inside of componentDidMount, which I guess runs once when the component is opened, I've also tried changing state inside of componentDidUpdate which then causes infinite loop.
I also left out of chunk of code which doesn't really have anything do with this problem just to make it somewhat tidier.
class SearchPage extends React.Component {
state = {
personId: '',
person_results: [],
}
componentDidMount(){
//this.props.fetchPersonInfo(this.state.personId)
let pid;
if(this.props.results.length > 0){
if (this.props.results[0].results[0].media_type === 'person'){
this.setState({person_results: this.props.results[0].results});
pid = this.state.person_results.id;
} else if (this.props.results[0].results[1].media_type === 'person'){
this.setState({person_results: this.props.results[0].results})
pid = this.state.person_results.id;
} else if (this.props.results[0].results[2].media_type === 'person'){
this.setState({person_results: this.props.results[0].results})
pid = this.state.person_results.id
}
}
this.setState({
personId: pid,
})
}
render(){
if (this.props.results.length > 0){
if (this.props.results[0].results[0].media_type === 'person'){
console.log(this.props.results[0].results[0]);
console.log(this.state.person_results);
return (
<div className='.col-md-4 search-container'>
<div className='search-results-person'>
<h5 className='search-title'>{this.state.person_results.name}</h5>
<img className='search-image' src={`${img_url}${this.state.person_results.profile_path}`} alt={this.state.person_results.name} />
</div>
</div>
)
} else if (this.props.results[0].results[1].media_type === 'person'){
console.log(this.state.person_results);
return (
<div className='.col-md-4 search-container'>
<div className='search-results-person'>
<h5 className='search-title'>{this.state.person_results.name}</h5>
<img className='search-image' src={`${img_url}${this.state.person_results.profile_path}`} alt={this.state.person_results.name} />
</div>
</div>
)
} else if (this.props.results[0].results[2].media_type === 'person'){
console.log(this.state.person_results);
return (
<div className='.col-md-4 search-container'>
<div className='search-results-person'>
<h5 className='search-title'>{this.state.person_results.name}</h5>
<img className='search-image' src={`${img_url}${this.state.person_results.profile_path}`} alt={this.state.person_results.name} />
</div>
</div>
)
}
}
return (
<div className='container-fluid'>
<div className='row'>
{results}
{this.state.personId}
{this.state.person_results}
</div>
</div>
)
}
}
const mapStateToProps = (state) => {
return {
results: state.movTv.results,
person: state.movTv.personInfo
}
}
export default connect(mapStateToProps, { fetchPersonInfo })(SearchPage);
EDIT: Updated code for the component.