使用布尔值 React Native 在我的屏幕中更改渲染

Change render in my screen with boolean React Native

我想在单击下一步按钮时更改同一页面中的渲染。在我的组件中,我的状态 next, setNext 初始化为 false。我的按钮的 onPress 我将状态更改为 true,但是当我单击下一个按钮时没有任何反应! 在这里你可以看到Step1 to 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>
     )}
   </>
 );
}

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

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()}
)