键盘每次关闭。

huangapple go评论60阅读模式
英文:

keyboard closes each time

问题

我在我的认证界面遇到了问题,当我尝试在TextInput中输入内容时,我的键盘关闭。

有人可以帮忙看看我的代码是否有错误吗?
我尝试更改了我的本地屏幕依赖项(版本3.10.1),但问题仍然存在。

import { createStackNavigator } from '@react-navigation/stack';
import React, { useState } from 'react';
import { View, Text, TextInput, TouchableOpacity, Image, StyleSheet } from 'react-native';

// Screens
import MainContainerScreen from '../MainContainer';
import RegisterScreen from './RegisterScreen';
import ForgotPasswordScreen from './ForgotPasswordScreen';

// Screen Names
const MainContainerName = "Main";
const registerName = "Inscription";
const forgotPasswordName = "Mot de passe oublié";

const Stack = createStackNavigator();

export default function LoginPage() {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');

  const handleLogin = (navigation) => {
    // Redirection vers l'interface MainContainer (TabNavigator)
    navigation.navigate(MainContainerName);
  };

  const handleForgotPassword = (navigation) => {
    // Redirection vers la page de réinitialisation du mot de passe
    navigation.navigate(forgotPasswordName);
  };

  const handleFirstTimeLogin = (navigation) => {
    // Redirection vers un autre écran pour la première connexion
    navigation.navigate(registerName);
  };

  function Login({ navigation }) {
    return (
      <View style={styles.container}>
        <Image source={require('../../assets/icon.png')} style={styles.logo} />

        <TextInput
          placeholder="Adresse e-mail"
          value={email}
          onChangeText={setEmail}
          style={styles.input}
        />

        <TextInput
          placeholder="Mot de passe"
          value={password}
          onChangeText={setPassword}
          secureTextEntry
          style={styles.input}
        />

        <TouchableOpacity onPress={() => handleLogin(navigation)} style={styles.button}>
          <Text style={styles.buttonText}>Connexion</Text>
        </TouchableOpacity>

        <TouchableOpacity onPress={() => handleForgotPassword(navigation)}>
          <Text style={styles.forgotPasswordText}>Mot de passe oublié</Text>
        </TouchableOpacity>

        <TouchableOpacity onPress={() => handleFirstTimeLogin(navigation)}>
          <Text style={styles.firstTimeLoginText}>Première connexion</Text>
        </TouchableOpacity>
      </View>
    );
  }

  return (
    <Stack.Navigator>
      <Stack.Screen name="Login" component={Login} options={{ headerShown: false }} />
      <Stack.Screen name={forgotPasswordName} component={ForgotPasswordScreen} options={{ headerShown: false }} />
      <Stack.Screen name={registerName} component={RegisterScreen} options={{ headerShown: false }} />
      <Stack.Screen name={MainContainerName} component={MainContainerScreen} options={{ headerShown: false }} />
    </Stack.Navigator>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  logo: {
    width: 150,
    height: 150,
    marginBottom: 50,
  },
  input: {
    width: '80%',
    height: 40,
    borderWidth: 1,
    borderColor: 'gray',
    borderRadius: 5,
    marginBottom: 10,
    paddingHorizontal: 10,
  },
  button: {
    backgroundColor: 'blue',
    width: '80%',
    height: 40,
    alignItems: 'center',
    justifyContent: 'center',
    borderRadius: 5,
    marginBottom: 10,
  },
  buttonText: {
    color: 'white',
    fontSize: 16,
  },
  forgotPasswordText: {
    color: 'blue',
    marginBottom: 10,
  },
  firstTimeLoginText: {
    color: 'blue',
  },
});
英文:

I have a problem with my authentification screen, when i try to put something in my TextInput my keyboard closes.

Can someone help and tell me if there is an error in my code.
I tried to change my native-screen dependency (version 3.10.1) and it's still the same problem.

import { createStackNavigator } from &#39;@react-navigation/stack&#39;;
import React, { useState } from &#39;react&#39;;
import { View, Text, TextInput, TouchableOpacity, Image, StyleSheet } from &#39;react-native&#39;;
// Screens
import MainContainerScreen from &#39;../MainContainer&#39;;
import RegisterScreen from &#39;./RegisterScreen&#39;;
import ForgotPasswordScreen from &#39;./ForgotPasswordScreen&#39;;
// Screen Names
const MainContainerName = &quot;Main&quot;;
const registerName = &quot;Inscription&quot;;
const forgotPasswordName = &quot;Mot de passe oubli&#233;&quot;;
const Stack = createStackNavigator();
export default function LoginPage() {
const [email, setEmail] = useState(&#39;&#39;);
const [password, setPassword] = useState(&#39;&#39;);
const handleLogin = (navigation) =&gt; {
// Redirection vers l&#39;interface MainContainer (TabNavigator)
navigation.navigate(MainContainerName);
};
const handleForgotPassword = (navigation) =&gt; {
// Redirection vers la page de r&#233;initialisation du mot de passe
navigation.navigate(forgotPasswordName);
};
const handleFirstTimeLogin = (navigation) =&gt; {
// Redirection vers un autre &#233;cran pour la premi&#232;re connexion
navigation.navigate(registerName);
};
function Login({ navigation }) {
return (
&lt;View style={styles.container}&gt;
&lt;Image source={require(&#39;../../assets/icon.png&#39;)} style={styles.logo} /&gt;
&lt;TextInput
placeholder=&quot;Adresse e-mail&quot;
value={email}
onChangeText={setEmail}
style={styles.input}
/&gt;
&lt;TextInput
placeholder=&quot;Mot de passe&quot;
value={password}
onChangeText={setPassword}
secureTextEntry
style={styles.input}
/&gt;
&lt;TouchableOpacity onPress={() =&gt; handleLogin(navigation)} style={styles.button}&gt;
&lt;Text style={styles.buttonText}&gt;Connexion&lt;/Text&gt;
&lt;/TouchableOpacity&gt;
&lt;TouchableOpacity onPress={() =&gt; handleForgotPassword(navigation)}&gt;
&lt;Text style={styles.forgotPasswordText}&gt;Mot de passe oubli&#233;&lt;/Text&gt;
&lt;/TouchableOpacity&gt;
&lt;TouchableOpacity onPress={() =&gt; handleFirstTimeLogin(navigation)}&gt;
&lt;Text style={styles.firstTimeLoginText}&gt;Premi&#232;re connexion&lt;/Text&gt;
&lt;/TouchableOpacity&gt;
&lt;/View&gt;
);
}
return (
&lt;Stack.Navigator&gt;
&lt;Stack.Screen name=&quot;Login&quot; component={Login} options={{ headerShown: false }}/&gt;
&lt;Stack.Screen name={forgotPasswordName} component={ForgotPasswordScreen} options={{ headerShown: false }}/&gt;
&lt;Stack.Screen name={registerName} component={RegisterScreen} options={{ headerShown: false }}/&gt;
&lt;Stack.Screen name={MainContainerName} component={MainContainerScreen} options={{ headerShown: false }}/&gt;
&lt;/Stack.Navigator&gt;
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: &#39;center&#39;,
justifyContent: &#39;center&#39;,
},
logo: {
width: 150,
height: 150,
marginBottom: 50,
},
input: {
width: &#39;80%&#39;,
height: 40,
borderWidth: 1,
borderColor: &#39;gray&#39;,
borderRadius: 5,
marginBottom: 10,
paddingHorizontal: 10,
},
button: {
backgroundColor: &#39;blue&#39;,
width: &#39;80%&#39;,
height: 40,
alignItems: &#39;center&#39;,
justifyContent: &#39;center&#39;,
borderRadius: 5,
marginBottom: 10,
},
buttonText: {
color: &#39;white&#39;,
fontSize: 16,
},
forgotPasswordText: {
color: &#39;blue&#39;,
marginBottom: 10,
},
firstTimeLoginText: {
color: &#39;blue&#39;,
},
});

答案1

得分: 1

在你的代码中,你在 Stack 组件内部创建了 Login 组件,并在 Stack 组件中声明了 email 和 password 状态。所以当你改变 email 或 password 状态时,Stack 组件会重新渲染,并且 Login 组件会被卸载(或完全销毁),然后重新挂载。这就是为什么键盘会消失的原因。所以为了使你的代码工作,你可以将 Login 组件移到 Stack 组件之外,并将密码和电子邮件状态以及所有导航处理程序放在 Login 组件内部。

以下是修正后的代码:

import { createStackNavigator } from "@react-navigation/stack";
import React, { useState } from "react";
import {
  View,
  Text,
  TextInput,
  TouchableOpacity,
  Image,
  StyleSheet,
} from "react-native";
import { NavigationContainer } from "@react-navigation/native";
// Screens
import MainContainerScreen from "./MainContainerScreen";
import RegisterScreen from "./RegisterScreen";
import ForgotPasswordScreen from "./ForgotPasswordScreen";

// Screen Names
const MainContainerName = "Main";
const registerName = "Inscription";
const forgotPasswordName = "Mot de passe oublié";

const Stack = createStackNavigator();

function Login({ navigation }) {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");

  const handleLogin = (navigation) => {
    // Redirection vers l'interface MainContainer (TabNavigator)
    navigation.navigate(MainContainerName);
  };

  const handleForgotPassword = (navigation) => {
    // Redirection vers la page de réinitialisation du mot de passe
    navigation.navigate(forgotPasswordName);
  };

  const handleFirstTimeLogin = (navigation) => {
    // Redirection vers un autre écran pour la première connexion
    navigation.navigate(registerName);
  };

  return (
    <View style={styles.container}>
      <Image source={require('../../assets/icon.png')} style={styles.logo} />

      <TextInput
        placeholder="Adresse e-mail"
        value={email}
        onChangeText={setEmail}
        style={styles.input}
      />

      <TextInput
        placeholder="Mot de passe"
        value={password}
        onChangeText={setPassword}
        secureTextEntry
        style={styles.input}
      />

      <TouchableOpacity
        onPress={() => handleLogin(navigation)}
        style={styles.button}
      >
        <Text style={styles.buttonText}>Connexion</Text>
      </TouchableOpacity>

      <TouchableOpacity onPress={() => handleForgotPassword(navigation)}>
        <Text style={styles.forgotPasswordText}>Mot de passe oublié</Text>
      </TouchableOpacity>

      <TouchableOpacity onPress={() => handleFirstTimeLogin(navigation)}>
        <Text style={styles.firstTimeLoginText}>Première connexion</Text>
      </TouchableOpacity>
    </View>
  );
}


export default function LoginPage() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen
          name="Login"
          component={Login}
          options={{ headerShown: false }}
        />
        <Stack.Screen
          name={forgotPasswordName}
          component={ForgotPasswordScreen}
          options={{ headerShown: false }}
        />
        <Stack.Screen
          name={registerName}
          component={RegisterScreen}
          options={{ headerShown: false }}
        />
        <Stack.Screen
          name={MainContainerName}
          component={MainContainerScreen}
          options={{ headerShown: false }}
        />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: "center",
    justifyContent: "center",
  },
  logo: {
    width: 150,
    height: 150,
    marginBottom: 50,
  },
  input: {
    width: "80%",
    height: 40,
    borderWidth: 1,
    borderColor: "gray",
    borderRadius: 5,
    marginBottom: 10,
    paddingHorizontal: 10,
  },
  button: {
    backgroundColor: "blue",
    width: "80%",
    height: 40,
    alignItems: "center",
    justifyContent: "center",
    borderRadius: 5,
    marginBottom: 10,
  },
  buttonText: {
    color: "white",
    fontSize: 16,
  },
  forgotPasswordText: {
    color: "blue",
    marginBottom: 10,
  },
  firstTimeLoginText: {
    color: "blue",
  },
});
英文:

In your code you have created the Login component inside your Stack Component and you declared the email and password states in the Stack Component so when you change the email or password states the Stack Component is re-rendered and along with that Login component unmounted (or destroyed completely) and mounted again. That's why the keyboard disappears. So in order for your code to work you can move Login Component outside the Stack Component and move the password and email state and all the navigation handlers inside the Login Component.

The corrected code is given below.

import { createStackNavigator } from &quot;@react-navigation/stack&quot;;
import React, { useState } from &quot;react&quot;;
import {
  View,
  Text,
  TextInput,
  TouchableOpacity,
  Image,
  StyleSheet,
} from &quot;react-native&quot;;
import { NavigationContainer } from &quot;@react-navigation/native&quot;;
// Screens
import MainContainerScreen from &quot;./MainContainerScreen&quot;;
import RegisterScreen from &quot;./RegisterScreen&quot;;
import ForgotPasswordScreen from &quot;./ForgotPasswordScreen&quot;;

// Screen Names
const MainContainerName = &quot;Main&quot;;
const registerName = &quot;Inscription&quot;;
const forgotPasswordName = &quot;Mot de passe oubli&#233;&quot;;

const Stack = createStackNavigator();

function Login({ navigation }) {
  const [email, setEmail] = useState(&quot;&quot;);
  const [password, setPassword] = useState(&quot;&quot;);

  const handleLogin = (navigation) =&gt; {
    // Redirection vers l&#39;interface MainContainer (TabNavigator)
    navigation.navigate(MainContainerName);
  };

  const handleForgotPassword = (navigation) =&gt; {
    // Redirection vers la page de r&#233;initialisation du mot de passe
    navigation.navigate(forgotPasswordName);
  };

  const handleFirstTimeLogin = (navigation) =&gt; {
    // Redirection vers un autre &#233;cran pour la premi&#232;re connexion
    navigation.navigate(registerName);
  };

  return (
    &lt;View style={styles.container}&gt;
      &lt;Image source={require(&#39;../../assets/icon.png&#39;)} style={styles.logo} /&gt;

      &lt;TextInput
        placeholder=&quot;Adresse e-mail&quot;
        value={email}
        onChangeText={setEmail}
        style={styles.input}
      /&gt;

      &lt;TextInput
        placeholder=&quot;Mot de passe&quot;
        value={password}
        onChangeText={setPassword}
        secureTextEntry
        style={styles.input}
      /&gt;

      &lt;TouchableOpacity
        onPress={() =&gt; handleLogin(navigation)}
        style={styles.button}
      &gt;
        &lt;Text style={styles.buttonText}&gt;Connexion&lt;/Text&gt;
      &lt;/TouchableOpacity&gt;

      &lt;TouchableOpacity onPress={() =&gt; handleForgotPassword(navigation)}&gt;
        &lt;Text style={styles.forgotPasswordText}&gt;Mot de passe oubli&#233;&lt;/Text&gt;
      &lt;/TouchableOpacity&gt;

      &lt;TouchableOpacity onPress={() =&gt; handleFirstTimeLogin(navigation)}&gt;
        &lt;Text style={styles.firstTimeLoginText}&gt;Premi&#232;re connexion&lt;/Text&gt;
      &lt;/TouchableOpacity&gt;
    &lt;/View&gt;
  );
}


export default function LoginPage() {
  return (
    &lt;NavigationContainer&gt;
      &lt;Stack.Navigator&gt;
        &lt;Stack.Screen
          name=&quot;Login&quot;
          component={Login}
          options={{ headerShown: false }}
        /&gt;
        &lt;Stack.Screen
          name={forgotPasswordName}
          component={ForgotPasswordScreen}
          options={{ headerShown: false }}
        /&gt;
        &lt;Stack.Screen
          name={registerName}
          component={RegisterScreen}
          options={{ headerShown: false }}
        /&gt;
        &lt;Stack.Screen
          name={MainContainerName}
          component={MainContainerScreen}
          options={{ headerShown: false }}
        /&gt;
      &lt;/Stack.Navigator&gt;
    &lt;/NavigationContainer&gt;
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: &quot;center&quot;,
    justifyContent: &quot;center&quot;,
  },
  logo: {
    width: 150,
    height: 150,
    marginBottom: 50,
  },
  input: {
    width: &quot;80%&quot;,
    height: 40,
    borderWidth: 1,
    borderColor: &quot;gray&quot;,
    borderRadius: 5,
    marginBottom: 10,
    paddingHorizontal: 10,
  },
  button: {
    backgroundColor: &quot;blue&quot;,
    width: &quot;80%&quot;,
    height: 40,
    alignItems: &quot;center&quot;,
    justifyContent: &quot;center&quot;,
    borderRadius: 5,
    marginBottom: 10,
  },
  buttonText: {
    color: &quot;white&quot;,
    fontSize: 16,
  },
  forgotPasswordText: {
    color: &quot;blue&quot;,
    marginBottom: 10,
  },
  firstTimeLoginText: {
    color: &quot;blue&quot;,
  },
});

huangapple
  • 本文由 发表于 2023年6月8日 18:45:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定