【问题标题】:React Native empty screen all the timeReact Native 一直空屏
【发布时间】:2017-10-23 10:31:42
【问题描述】:

我是 react-native 的新手,试图创建自己的组件,但它一直显示一个空白屏幕。

这是我的组件代码

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>)
    }
}

【问题讨论】:

  • componentMounted()?你的意思是componentDidMount()

标签: android ios react-native


【解决方案1】:

如果您在组件安装后尝试获取一些数据,请将您的代码更改为此

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>)
    }
}

希望对您有所帮助。

【讨论】:

  • 谢谢你让我开心。
猜你喜欢
  • 2016-03-07
  • 1970-01-01
  • 2017-10-17
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-10
相关资源
最近更新 更多