react native - Undefined is not an object (evaluating 'this.fetchData().then') -
native function draw scene inside main render. error title of question ( undefined not , object...)
this function block :
fetchdata() { const { params } = this.props.navigation.state; fetch(request_url+'&'+'cabnum='+params.cabreg) .then((response) => response.json()) .then((responsedata) => { //alert(params.cabreg); alert.alert('','your driver :'+responsedata[0].driver); return responsedata; }).done(); }
and main render :
render() { const { params } = this.props.navigation.state; return ( <view> <text>{params.cabreg}</text> <view>{this.fetchdata() .then((responsedata)=>{ return(<view style={styles1.rightcontainer}> <image source={{uri: responsedata[0].image}} style={styles1.thumbnail} /> <view style={{flexdirection:'column',alignitems:'center'}}> <text style={styles1.title}>{responsedata[0].reg}</text> <text style={styles1.driver}>driver:{responsedata[0].driver}</text> <text style={styles1.driver}>cab type:{responsedata[0].ctype}</text> <text style={styles1.driver}>contact:{responsedata[0].contact}</text> </view> </view> ); } ) } </view> </view> ); }
this not correct way of rendering response data view.
try this:
render() { const { params } = this.props.navigation.state return ( <view> <text> {params.cabreg} </text> <view> {this.fetchdata()} </view> </view> ) }
and function :
fetchdata() { const { params } = this.props.navigation.state fetch(request_url + '&' + 'cabnum=' + params.cabreg) .then(response => response.json()) .then(responsedata => { //alert(params.cabreg); alert.alert('', 'your driver :' + responsedata[0].driver) return ( <view style={styles1.rightcontainer}> <image source={{ uri: responsedata[0].image }} style={styles1.thumbnail} /> <view style={{ flexdirection: 'column', alignitems: 'center' }}> <text style={styles1.title}> {responsedata[0].reg} </text> <text style={styles1.driver}> driver:{responsedata[0].driver} </text> <text style={styles1.driver}> cab type:{responsedata[0].ctype} </text> <text style={styles1.driver}> contact:{responsedata[0].contact} </text> </view> </view> ) }) .done() }
Comments
Post a Comment