简体   繁体   中英

global event listener for button clicks in a mobile app (android/ios)

I want to track all the button clicks that happen within my app and generate (for example) an alert as a response to each. Is there a way to set up some sort of a "global" event listener for button clicks that would allow me to know when a certain button was clicked and give me some type of identification of what element was clicked?

Also, I was wondering if this feature would be specific for android/iOS apps or could it be general?

You can have a file function.js and have the function inside it . Import and call this function from your component's button onPress and pass the required data as argument.

The function would be generic. You can make it specific by if else condition based on the platform, ie Android/Ios

Function.js

import { Platform } from 'react-native'

export function globalFunc()
{
   if(Platform.OS == 'ios')
      alert("ios")
   else
      alert("android")
}

component.js

import {globalFunc()} from './Function.js'

render()
{
  return(
    <TouchableOpacity onPress={()=>globalFunc()}>Button</TouchableOpacity>
  )
}

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