skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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 ?

    const [touchableColor, setTouchableColor] = useState("blue")
    
    <TouchableOpacity style={{backgroundColor: touchableColor}} onPress={() => setTouchableColor("red")} >  // ERROR comes
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search