skip to Main Content

this below code isn’t work, please tell me whats wrong with this code?

import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View,Button } from 'react-native';



export default function App() {
    handle()
    {
        console.log("pressed");
    }
    return (
        <View style={styles.container}>
            <Text>HEllo World!</Text>
            <Button variant="outlined" title = " hello" onPress={handle}></Button>
            <StatusBar style="auto" />
        </View>
           );
  
}

const st
yles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

I’m just try to print some text in console, when the button is pressed.

2

Answers


  1. just change it to:

     function handle()
        {
            console.log("pressed");
        }
    

    and then:

    onPress={()=>handle()}
    
    Login or Signup to reply.
  2. here is the working code!

    you can’t use function like this way

     handle(){
        alert('Presssed');
        console.log('pressed');
      };
    

    it’s not working so can use like this way

        const handle = () => {
        alert('Presssed');
        console.log('pressed');
      };
    

    App.js

    import React from 'react';
    import { StyleSheet, Text, View, Button } from 'react-native';
    
    export default function App() {
      const handle = () => {
        alert('Presssed');
        console.log('pressed');
      };
    
      return (
        <View style={styles.container}>
          <Text>HEllo World!</Text>
          <Button variant="outlined" title=" hello" onPress={handle}></Button>
        </View>
      );
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center',
      },
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search