リアクションネイティブアプリでデータを使用するためにAPIにクエリを実行しようとしていますが、 。基本的に、私は状態に格納されている市場オブジェクトにアクセスしようとしています。状態を調べると、マーケットオブジェクトが適切に設定されていることがわかります。しかし、マーケットオブジェクトで何かしようとするとすぐに、私はCannot read property 'markets' of null
というエラーが発生します。リアクションネイティブオブジェクトは状態にありますが、アクセスしようとすると未定義です
なぜこのようなことが起こり、この問題を解決するにはどうすればよいですか?
以下に問題を引き起こすコードが含まれています。私は何か提案を感謝します。
export default class cryptoTracker extends Component {
constructor(props) {
super(props);
this.opts = {
baseUrl: 'https://bittrex.com/api/v1.1',
apikey: 'APIKEY',
apisecret: 'APISECRET',
};
this.updateMarkets();
}
updateMarkets(){
axios.get(this.opts.baseUrl + '/public/getmarkets').then((response) => {
this.setState({'markets' : response.data.result});
}
).catch((error) => {
console.log(error.name)
console.log("Error");
});
}
render() {
console.log("The state is below");
console.log(this.state);
//This line causes the issue
console.log(this.state.markets);
return(
<View>
</View>);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('cryptoTracker',() => cryptoTracker);
コンソール出力を貼り付けることはできますか? –