【问题标题】:Change render in my screen with boolean React Native使用布尔 React Native 在我的屏幕上更改渲染
【发布时间】:2021-06-23 10:01:06
【问题描述】:

当我点击下一个按钮时,我想在同一页面中更改我的渲染。在我的组件中,我的状态 next, setNext 初始化为 false。 onPress 到我的按钮我将状态更改为 true,但是当我单击下一个按钮时没有任何反应! 在这里你可以看到Step1 到Step2 并了解更多我需要什么^^

const [next, setNext] = useState(false);

{!next ? (
  //Step1 
  <BigButton
    title="Suivant"
    onPress={() => {
    setNext(true);
   }}
  />) : ( // Step2 )
}

我所有的代码都在这里:


import React, { useState } from "react";
import { useNavigation } from "@react-navigation/core";

import BigButton from "../components/BigButton";
import BigTextInfos from "../components/BigTextInfos";

export default function SignUpScreen({ setToken }) {
 const [next, setNext] = useState(false);
 const [showPassword, setShowPassword] = useState(false);

 const toggleSwitchPassword = () => {
   setShowPassword((prev) => !prev);
 };


 return (
   <>
     {!next ? ( // STEP 1
       <View style={{ marginTop: 60 }}>
         <BigTextInfos title="S'inscrire" subtitle="Vous êtes :" />
         <View>
           <View>
             <TouchableHighlight
               style={[styles.input, styles.inputGender]}
               onPress={() => setGender("Mme")}
             >
               <Text style={[styles.darkGrey]}>Mme</Text>
             </TouchableHighlight>
             <TouchableHighlight
               style={[styles.input, styles.inputGender]}
               onPress={() => setGender("M.")}
             >
               <Text style={[styles.darkGrey]}>M.</Text>
             </TouchableHighlight>
           </View>
           <TextInput
             style={[styles.input]}
             placeholder="Prénom"
             placeholderTextColor={colors.darkgrey}
           />
           <TextInput
             style={[styles.input]}
             placeholder="Nom"
             placeholderTextColor={colors.darkgrey}
           />

           <BigButton
             title="Suivant"
             onPress={() => {
               setNext(true);
             }}
           />
         </View>
       </View>
     ) : ( // STEP 2
       <View style={{ marginTop: 60 }}>
         <BigTextInfos title="S'inscrire" subtitle="Finalisez l'inscription" />
         <View>
           <TextInput
             style={[styles.input]}
             placeholder="Email"
             placeholderTextColor={colors.darkgrey}
           />
           <View style={[styles.input]}>
             <TextInput
               style={{ width: 300 }}
               placeholder="Mot de passe"
               placeholderTextColor={colors.darkgrey}
               color={colors.dark}
               secureTextEntry={showPassword ? true : false}
             />

             {showPassword ? (
               <Feather
                 name="eye-off"
                 size={24}
                 color={colors.grey}
                 onPress={() => {
                   toggleSwitchPassword();
                 }}
               />
             ) : (
               <Feather
                 name="eye"
                 size={24}
                 color={colors.grey}
                 onPress={() => {
                   toggleSwitchPassword();
                 }}
               />
             )}
           </View>
           <BigButton
             title="S'inscrire"
             empty="false"
             onPress={async () => {
               console.log("s'inscrire");
               const userToken = "secret-token";
               setToken(userToken);
             }}
           />
         </View>
       </View>
     )}
   </>
 );
}

【问题讨论】:

  • 您的示例与您的描述不符。当您将 next 初始化为 false 时,您开始询问是否为 true,这意味着大按钮永远不会呈现。您的示例应该询问它是否为假,或者您应该在冒号之后呈现按钮。
  • 是的,抱歉,这是!next 而不是next!谢谢,但是当我点击我的 btn 时,没有任何反应:(

标签: javascript react-native jsx react-native-ios


【解决方案1】:

它不起作用有点奇怪,尝试不同的方法 - 将返回逻辑分离在一个函数中并在渲染时调用该函数。它应该看起来有点像这样

const [next,setNext] = useState(false)

const handleSteps = () => {
 if(next){
  return <BigButton
     title="Suivant"
     onPress={() => {
     setNext(true);
     }}/>
 }
 return <SmallButton /> //here goes the other component
}

然后在 return 中调用该 handleSteps 函数

return(
{handleSteps()}
)

【讨论】:

  • 感谢您的帮助!但这不起作用:(所以我认为它不是只用状态渲染动态组件的方式......我想我会把我所有的代码都放在这里^^也许我错过了一些东西,因为React也使用状态......我真的不知道!!
  • 我只是给你更多的代码。我不知道这是否能帮助你!例如,我使用密码“玩”读取或不读取安全参数,并且仅使用有效的布尔值!
  • snack.expo.io/vvoMJFekC 这是一个基本的例子:)
猜你喜欢
  • 2018-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-20
  • 2020-10-05
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多