简体   繁体   中英

How to access this.state from nested objects in react-native?

I have a simple component like so:

 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); } }

How can I access the data array within the state inside load function?

Note

I don't want to bind the load().bind(this) function.

Thanks for the consideration.

Edit

This question is different, in that we're dealing with a scope that we're forced not to use this keyword at all. Please check my updated question above.

One approach: In the first line of your render function, you can save a reference to the state .

const state = this.state;

Then just reference component state using the constant state .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM