I am new to react and working on a project in react native.(我是新来做出反应的人,并在React Native中从事项目。)
*ngIf
componentDidMount() { this._fetchData(); } _fetchData = () => { if (this.state.loading) { return; } this.setState({ loading: true }); let typr = this._returnReqType(this.state.requestType) let include = [ 'application', 'association:id,name', 'unit:id,unit_number', 'status_history', 'status_history.user', ]; EservicesService.getEservicesRequestDetails(this.state.requestId, typr, include).then(response => { console.log(response.record); // {prop1:'XXXXXXX',association:{name:'Association 1', ...}, ... } console.log(response.record.association); // {name:'Association 1', ...} this.setState({ error: response.error || null, loading: false, request: response.record }); }).catch(error => { this.setState({ error, loading: false }); }); }
render() { console.log(this.state.request); // {prop1:'XXXXXXX',association:{name:'Association 1', ...}, ... } console.log(this.state.request.association); // {name:'Association 1', ...} return ( <View style={{ flex: 1 }}> <ScrollView> <Card > <Text>Association: {this.state.request.association_id}</Text> <Text>Unit: {this.state.request.association.name}</Text> {// Error Here "Cannot read 'name' of undefined" } </Card> </ScrollView> </View> );}
You need to check if the name is available, then render it.(您需要检查名称是否可用,然后进行渲染。)
{ this.state.request.association && <Text> Unit: {this.state.request.association.name && this.state.request.association.name} </Text> }
2.1m questions
2.1m answers
60 comments
57.0k users