How to fix onclick function is not working in a React Native app?

Sometimes, we want to fix onclick function is not working in a React Native app.

In this article, we’ll look at how to fix onclick function is not working in a React Native app.

How to fix onclick function is not working in a React Native app?

To fix onclick function is not working in a React Native app, we should replace onclick with onPress.

For instance, we write:

import * as React from 'react';
import { TouchableOpacity, Text, View, StyleSheet } from 'react-native';

export default function App() {
  const onPress = () => console.log('pressed');

  return (
    <View>
      <TouchableOpacity onPress={onPress}>
        <View>
          <Text>...</Text>
        </View>
      </TouchableOpacity>
    </View>
  );
}

to set the onPress prop to the onPress function.

Now when we press the text, we should see 'pressed' logged.

Conclusion

To fix onclick function is not working in a React Native app, we should replace onclick with onPress.