【问题标题】:React Native Buttons onPress nothing happens, also onChangeText not workingReact Native Buttons onPress 没有任何反应,onChangeText 也不起作用
【发布时间】:2020-07-04 10:29:41
【问题描述】:

我是 React Native 的新手。我有一个包含两个字段的登录表单。我想在将用户发送到 Dashboard.js 之前对用户进行身份验证。无论我尝试什么,按钮都没有做任何事情(绝对没有任何反应,没有错误)。我将代码保留在 Login.js 中,以显示我尝试做的所有事情

  1. 路由到 dashboard.js
  2. 试图通过在按钮按下时调用 handleSubmitPress 函数来做某事。
  3. 弹出警报。

我使用了两个按钮。一份来自 react-native-paper,一份来自 react-native。 我在下面给出了我的 app.js 和 login.js 的代码。请问有人可以帮忙吗?

代码

  App.js
    import React from "react";
    import { NavigationContainer } from "@react-navigation/native";
    import { createStackNavigator } from "@react-navigation/stack";
    import DashBoard from "./mainmenu/DashBoard";
    import Login from "./mainmenu/Login";
    
    const Stack = createStackNavigator();
    
    export default function App() {
      return (
        <NavigationContainer>
          <Stack.Navigator>
            <Stack.Screen name="Login" component={Login} />
            <Stack.Screen name="DashBoard" component={DashBoard} />
          </Stack.Navigator>
        </NavigationContainer>
      );
    }
    
    
    Login.js
    import React, { useState } from "react";
    import { Title, TextInput} from "react-native-paper";
    import { View, Alert,Button } from "react-native";
    export default function Login() {
      const [mobile, setMobile] = useState("123456789");
      const [password, setPassword] = useState("123");
    
      function handleSubmitPress() {
        console.log({ mobile, password });
      }
    
      return (
        <View
          style={{
            backgroundColor: "skyblue",
            alignItems: "center",
            justifyContent: "center",
          }}
        >
          <TextInput
            label="Your mobile number "
            value={mobile}
            onChangeText={(text) => setMobile(text)}
          />
          <TextInput
            label="Type Password  "
            value={password}
            onChangeText={(text) => setPassword(text)}
          />
    
          <Button
            icon="camera"
            type="submit"
            mode="contained"
            //onPress={() => props.navigation.navigate("DashBoard")}
            onPress={()=>Alert.alert('Navigate pressed')}
          >
            Navigate
          </Button>
    
          <Button
              title="Print"
              onPress={() => Alert.alert('Simple Button pressed')}
              // onPress={handleSubmitPress()}
            />
        </View>
      );
    }

【问题讨论】:

    标签: react-native routes expo react-native-paper


    【解决方案1】:

    将您的登录组件更改为此。

    ...
    const Login = ({navigation}) => {
      const [mobile, setMobile] = useState('123456789');
      const [password, setPassword] = useState('123');
    
      function handleSubmitPress() {
        console.log({mobile, password});
      }
    
      return (
        <View
          style={{
            backgroundColor: 'skyblue',
            alignItems: 'center',
            justifyContent: 'center',
          }}>
          <TextInput
            label="Your mobile number "
            value={mobile}
            onChangeText={text => setMobile(text)}
          />
          <TextInput
            label="Type Password  "
            value={password}
            onChangeText={text => setPassword(text)}
          />
    
          <Button
            title="title"
            icon="camera"
            type="submit"
            mode="contained"
            onPress={() => navigation.navigate('DashBoard')}>
            Navigate
          </Button>
    
          <Button title="Print" onPress={() => handleSubmitPress()} />
        </View>
      );
    };
    
    export default App;
    
    

    有几个问题。

    我使用了默认的 React Native 按钮。所以我在其中一个按钮上添加了一个标题道具,因为这是必需的。我给它的值是“title”,但是把它改成你想要的或者使用 React Native Paper 按钮。

    主要问题是您如何在按钮onPress 中调用handleSubmitPress

    你的 onChangeText 很好,你只是看不到结果,因为 handleSubmitPress 没有被调用。

    我还使用了解构,因此您可以直接访问导航道具。您也可以使用const Long = (props) =&gt; {...} 并使用props.navigation,但无论哪种方式,您都需要传递一些内容,否则导航将无法正常工作。

    【讨论】:

      猜你喜欢
      • 2018-09-19
      • 2021-07-24
      • 2018-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多