Sono nuovo a React
e React native
e sto provando a retrieve data from an API
e quindi lo eseguo ma mi sembra che si stiano incontrando problemi.Ottenere undefined non è un oggetto in React native durante il rendering
Posso prendere i dati dal API
OK, ma quando provo e render
mi sto ricevendo tutti i tipi di errori.
In sostanza, tutto ciò che sto cercando di eseguire è il rendering delle foto restituite dall'API. Dovrebbe essere semplice, giusto? Gradirei chiunque possa indicarmi la strada giusta.
che sto ricevendo errori come:
indefinita non è un oggetto (la valutazione 'this.props.photos') in RenderPhotos_render
Forse ho saltato in React Native
troppo presto .. . Scusa la mia mancanza di conoscenza!
var AwesomeProject = React.createClass({
getInitialState: function() {
return {
isLoading: true,
photos: this.props.photos
};
},
componentWillMount: function(){
fetch("http://localhost:3000/api/photos/", {
method: "GET",
headers: {
"x-access-token":"xxxxx",
"x-key":"xxxx"
},
})
.then((response) => response.json())
.then((responseData) => {
AlertIOS.alert(
"GET Response",
"Search Query -> " + JSON.stringify(responseData)
)
this.setState({
isLoading: false
});
this.setState({
photos: JSON.stringify(responseData)
});
})
.done();
},
render: function() {
if(this.state.isLoading){
return <View><Text>Loading...</Text></View>
}
return (
<RenderPhotos photos={this.props.photos}/>
);
},
});
var RenderPhotos = React.createClass({
getInitialState: function() {
return {
photos: this.props.photos
};
},
render: function(){
var photos = Photos;
return (
<View>
<Text> {this.props.photos[0]} </Text>
</View>
)
}
});
Una piccola modifica è che si potrebbe cadere lo stato da RenderPhotos dal momento che non è più lo si utilizza – rmevans9
Anche i var = foto Foto è inutile nella funzione di rendering. E infine this.props.photos inizia come indefinito (forse, lo stato è impostato da una proprietà che non viene mostrata) che finirebbe con un errore indefinito dal momento che si va direttamente a provare a visualizzare l'indice 0 dell'array. – rmevans9