how to setState in nested objec get function

55 Views Asked by At

i'm using react-native-svg-charts to dynamically display a chart of income and savings, when the onPress function is called i want to change the value of the current state, but i'm unable to bind the context, so this.setState is undefined. how can i bind the context so the onPress function is able to correctly setState?

my code:

for (var i = 0; i < dias; i++) {

            var valueIncome= Math.floor(Math.random() * 10000)
            var valueSaved= Math.floor(Math.random() * 10000)
            var obj = {

                income: {
                    value: valueIncome,
                    allValues: [valueIncome, valueSaved],
                    position: i,
                    get svg() {
                        return { onPress: () => console.log(this.position) }
                    },
                },
                saved: {
                    value: valueSaved,
                    allValues: [valueIncome, valueSaved],
                    position: i,
                    get svg() {

                        return { onPress: () => console.log(this.position) }
                    },
                },
                

            }

            data.push(obj)
        }
0

There are 0 best solutions below