Waqas Baloch
Waqas Baloch

Reputation: 23

React Native empty screen all the time

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

Answers (1)

Azeem Hassni
Azeem Hassni

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

Related Questions