我正面临一个奇怪的问题.在我的react-native 应用程序中,我有customAlert个,它显示如果该语句为false,则当前只显示console.

enter image description here

    import CustomAlert from '../components/CustomAlert';
    const [modalVisible, setModalVisible] = useState(false);

    <View style={{ marginTop: `30%`, alignItems: 'center' }}>
        <GoogleSigninButton
          style={{ width: 252, height: 58 }}
          size={GoogleSigninButton.Size.Wide}
          color={GoogleSigninButton.Color.Dark}
          onPress={() => {
            if (fingerprint === true) {
              googleLogin();
            }
            else {
              console.log("Alert should pop up");

              <CustomAlert
                modalVisible={modalVisible}
                setModalVisible={setModalVisible}
                title={'Message'}
                message={'Please enable your Touch ID/PIN to your device'}
                buttons={[{
                  text: 'Ok',
                  func: () => { console.log('Yes Pressed') }
                }]}
              />

            }
          }
          }
        />
    </View>

推荐答案

您可以try 将<CustomAlert>移出<GoogleSignInButton>,然后根据modalVisible状态变量有条件地显示<CustomAlert>:

import CustomAlert from '../components/CustomAlert';
const [modalVisible, setModalVisible] = useState(false);

<View style={{ marginTop: `30%`, alignItems: 'center' }}>
    <GoogleSigninButton
      style={{ width: 252, height: 58 }}
      size={GoogleSigninButton.Size.Wide}
      color={GoogleSigninButton.Color.Dark}
      onPress={() => {
        if (fingerprint === true) {
          googleLogin();
        }
        else {
          console.log("Alert should pop up");
          setModalVisible(true);
        }
      }
      }
    />
    <CustomAlert
        modalVisible={modalVisible}
        setModalVisible={setModalVisible}
        title={'Message'}
        message={'Please enable your Touch ID/PIN to your device'}
        buttons={[{
          text: 'Ok',
          func: () => { console.log('Yes Pressed') }
        }]}
    />
</View>

注意,在else分支中,我们调用setModalVisible(true),它将modalVisible设置为true.然后modalVisible作为props 传递给<CustomAlert>,这应该告诉模态渲染(假设设置正确).

Javascript相关问答推荐

Vue-Router渲染组件但不更改网址

使用typeof运算符获取对象值类型-接收字符串而不是数组

D3多线图显示1线而不是3线

将自定义排序应用于角形数字数组

html + java script!需要帮助来了解为什么我得到(无效的用户名或密码)

使用Java脚本导入gltf场景并创建边界框

正则表达式,允许我匹配除已定义的子字符串之外的所有内容

如何将innerHTML字符串修剪为其中的特定元素?

Web Crypto API解密失败,RSA-OAEP

环境值在.js文件/Next.js中不起作用

扩展类型的联合被解析为基类型

未加载css colored颜色 ,无法将div设置为可见和不可见

SPAN不会在点击时关闭模式,尽管它们可以发送日志(log)等

当代码另有说明时,随机放置的圆圈有时会从画布上消失

如何在Press上重新启动EXPO-AV视频?

在Odoo中如何以编程方式在POS中添加产品

为什么我不能使用其同级元素调用和更新子元素?

将Singleton实例设置为未定义后的Angular 变量引用持久性行为

Angel Auth Guard-用户只有在未登录时才能访问登录页面,只有在登录时才能访问其他页面

我在哪里添加过滤器值到这个函数?