Today, I encountered such a demand at the time of writing code - need to add a variable to the variable name.
At this time we can use eval () function.
The eval js () function computes a string, and executes the JavaScript code.
Chestnuts as follows:
{ xxx.map((item, index) => { return <ScrollView onLayout={event => { eval(`agencyY${index} = event.nativeEvent.layout.y`) }} key={index}> blabla~~~ </ScrollView> }) }
The above code eval ( `agencyY $ {index} = event.nativeEvent.layout.y`), equivalent defined agencyY0 = event.nativeEvent.layout.y, agencyY1 = event.nativeEvent.layout.y, ......... ...
END-------------------