Is it possible in React Native to set the style of a button by using e.currentTarget
method? Since there is a style
object inside the currentTarget
object. I mean:
<TouchableOpacity onPress={(e) => e.currentTarget.style.color="red"} > // ERROR comes
2
Answers
In React Native, TochableOpacity component doesn’t work exactly like the native elements in the web environment. Therefore you can’t directly set styles using
e.currentTarget.style
as you might in a web-based react application.Set the color pop with
useState
and pass it to the component style prop.You should avoid direct manipulation.
Why you don’t want use
useState
?