I have a component representing a user avatar that loads an image from my API. I want it to display the default avatar (not another image) while loading the avatar.
constructor() { super(); this.state = { loaded: false, }; } render() { if (!this.props.uri || !this.state.loaded) { return ( <DefaultAvatar /> ); } return <Image onLoad={this.onLoad.bind(this)} uri={this.props.uri} />; } onLoad() { this.setState({loaded: true}); }
The problem is that with this current code, Image will never be displayed, so the state will never change. I canβt find a solution that satisfies the React principles and my requirements (without ghost components to download the image before displaying it).
source share