如何在 React Native 中使用 Button 的 onPress 属性?

问题描述 投票:0回答:1

下面的代码不起作用,请告诉我这段代码有什么问题?

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',
  },
});

我只是尝试在按下按钮时在控制台中打印一些文本。

reactjs react-native button text
1个回答
0
投票

只需将其更改为:

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

然后:

onPress={()=>handle()}
© www.soinside.com 2019 - 2024. All rights reserved.