I'm having a trouble implementing shouldComponentUpdate
on my React app with loadingData
state. I want to prevent component to load again when nextProps has not changed from the this.props which is working fine and it messes up loadingData
value some how and I can't find the reason why.
Why loadingData
ended up to be true
even there's no change to the data in Redux(nextProps and this.props)?
constructor(props){
super(props)
this.state = {
loadingData: false,
}
}
async componentDidMount() {
this._isMounted = true;
const { username } = this.state
this.setState({ loadingData: true })
try {
await this.props.getUserData(username)
this.setState({ loadingData: false })
} catch (err) {
console.log(err)
this.setState({ loadingData: false })
}
}
shouldComponentUpdate(nextProps, nextState) {
return this.props !== nextProps
}
render() {
return(
<div>{this.state.loadingData ? "Loading..." : this.props.userData}</div>
)
}
Updated code to show the how I set up loadingData
state. Some reason, adding shouldComponentUpdate
shows, Loading... on the screen instead of userData from Redux. I'm not sure why...