[英]How to access this.state from nested objects in react-native?
我有一個像這樣的簡單組件:
import React, { Component } from 'react'; import { StyleSheet, Text, View } from 'react-native'; export default class Statistics extends Component { constructor(props) { super(props); this.state = { data: [] } } componentDidMount() { } render() { var config = { chart: { events: { load: function() { console.log(this.state.data); } } } }; return (null); } }
如何在加載函數內的狀態中訪問數據數組?
我不想綁定load().bind(this)
函數。
感謝您的考慮。
這個問題是不同的,因為我們正在處理一個我們被迫根本不使用 this 關鍵字的范圍。 請檢查我上面更新的問題。
一種方法:在渲染函數的第一行,您可以保存對state
的引用。
const state = this.state;
然后只需使用常量state
引用組件state
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.