javascript - React-native 和 AXIOS 上的条件渲染问题

标签 javascript reactjs react-native expo

我在 native react 中有一个大问题,我有这个:

getreportbycode = async() => {
        this.setState({show : false})
        if(this.state.finder !== ''){
            const token = await AsyncStorage.getItem('petra-token');
            this.setState({ jwt: token, sendform: true });
            axios.post('https://petrapi.herokuapp.com/emergence/find', {
                report_id : this.state.finder
        }, {headers: {
                Authorization: `Bearer ${this.state.jwt}`
        }
            }).then(response => {
                this.setState({report: response.data.data, sendform:false})
                console.log(this.state.report)
                this.setState({mostrar : true})

        }).catch(error => {
            this.setState({finder:'', sendform:false})
            console.log(error)
        })
        }
    }

还有这个:

report(report){
        if(this.state.show == true){
            return(
                <Card>
                    <CardItem>
                            <Left>
                            { report.alert.image == null
                            ?
                            <Thumbnail source={{uri:
                            'https://cdn2.iconfinder.com/data/icons/user-icon-2-1/100/user_5-15-512.png' }}
                            
                                style={{height: 50, width: 50}}/>
                            :
                            <Thumbnail source={{uri: report.alert.image}}
                            style={{height: 50, width: 50}}/>
                            }
                        </Left>
                        <Body>
                            <Text>
                                Creador: {report.user.name}
                            </Text>
                            {this.proccesbar(report.status)}
                        </Body>
                    </CardItem>
                    </Card>
)}

问题是只有当我 try catch “错误”时,我才会收到此错误消息:

only when i tried catch error

有什么方法可以正确地做到这一点吗?

最佳答案

似乎您正在尝试获取 undefined object 的图像值。 因此,您不仅可以检查 report.alert.image 是否为 null,还可以验证 reportalert > 对象具有值(value),例如:

    (...)
    {
      (report?.alert?.image) ?
         <Thumbnail source={{uri: report.alert.image}} style={{height: 50, width: 50}}/>
      :
         <Thumbnail source={{uri: 'https://cdn2.iconfinder.com/data/icons/user-icon-2-1/100/user_5-15-512.png' }} style={{height: 50, width: 50}}/>
    }
    (...)

关于javascript - React-native 和 AXIOS 上的条件渲染问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59515680/

相关文章:

javascript - 联系表 7 : Not routing to new page

javascript - 使用 React 提交表单,无需状态

android - 如何使用 fetch(GET Call) 设置超时 react-native api 调用?

javascript - 安装 React Native map 。在 UIManager 中找不到 AIRMap

java - 将现有的 Java 代码与 React Native 结合使用

javascript - 隐藏/显示子菜单

javascript - 如何将 Bootstrap 模式绑定(bind)到使用 Bootstrap Table 呈现的操作按钮

javascript - 获取下拉标题以更改为选定的下拉值

css - 滚动不工作 : react-grid-layout: 0-showcase example

javascript - React 组件投票向上和向下