React Native 中警报中的组件

Component inside alert in react native

大家好,我需要在警报中放置一个组件(开关),

export const secretAlert = (environment: object) => {
  Alert.alert(
    'Alert Title',
    'My Alert Msg',
    [
      {
        text: `Environment: ${Config.ENV}`,
      },
      {
        text: 'Cancel',
        onPress: () => console.log('Cancel Pressed'),
        style: 'cancel',
      },
      { text: 'OK', onPress: () => console.log('OK Pressed') },
    ],
    { cancelable: false },
  );
    return (
          <Text>test</Text>,
    )
}

第二次尝试

export const secretAlert = (environment: object) => {
  Alert.alert(
    'Alert Title',
    'My Alert Msg',
    [
      {
        text: `Environment: ${Config.ENV}`,
      },
      {
        text: 'Cancel',
        onPress: () => console.log('Cancel Pressed'),
        style: 'cancel',
        switch: <ComponentHere>. 
      },
      { text: 'OK', onPress: () => console.log('OK Pressed') },
    ],
    { cancelable: false },
  );
}

上面的代码是错误的,只是为了让你知道我要做什么。 有一种方法可以使用默认组件警报或我应该使用一些 library/custom 组件吗?

默认的 react-native Alert 无法做到这一点。您可以使用此 library and use Dialog.Switch or I recommend that you use a modal 高度自定义您的提醒。