简体   繁体   中英

How to activate the TouchablePpacity onPress from javascript in react native?

I'm new to react native and I don't know how to auto click a TouchableOpacity .

With jquery and HTML it would be $("#id").click(); but in react native I don't know how to manipulate the onPress with javascript so that it is pressed alone.

 import {TouchableOpacity }  from 'react-native';
...
 const function=()=>{
      console.log('pressed!');
    };
    ...
    <TouchableOpacity onPress={()=>function()}>
       <Text> Press me!<Text/>
    <TouchableOpacity/>

Try this and let me know!

I advise you should call a function, Not a trigger:

    <TouchableOpacity onPress={()=>yourfunction()}>
    </TouchableOpacity>

If you insist on trigger on touchables... you should use useRef hook or ref,

See this

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