Reputation: 23
I'm new to react-native trying to create my own component but it shows an empty screen all the time.
here is my component code
class BoxComponent extends Component {
constructor(props){
super(props);
this.state = {message: null}
}
componentMounted(){
axios.get('example.com').then(response => {
this.setState({
message: response.data
})
})
}
render(){
return (<Text style={style.boxStyle}>{this.state.message}</Text>)
}
}
Upvotes: 2
Views: 94
Reputation: 886
If you are trying to fetch some data after the component gets mounted then change your code to this
class BoxComponent extends Component {
constructor(props){
super(props);
this.state = {message: null}
}
componentDidMount(){
axios.get('example.com').then(response => {
this.setState({
message: response.data
})
})
}
render(){
return (<Text style={style.boxStyle}>{this.state.message}</Text>)
}
}
Hope that helps.
Upvotes: 1