【问题标题】:how can i make on alert button in react-native?如何在 react-native 中设置警报按钮?
【发布时间】:2021-04-30 15:35:24
【问题描述】:

我想要一个按钮使用警报。但是,两个按钮 (createTwoButtonAlert) 可以正常工作,但是当我尝试创建一个按钮 (createOneButtonAlert) 时出现此错误。

消息的值不能从 readablenativemap 转换为字符串

这是我的代码

    import React, {useState} from 'react';
    import {View, StyleSheet, Button, Alert} from 'react-native';

    const Third = () => {
      const createOneButtonAlert = () =>
        Alert.alert('Alert Title', [
          {text: 'OK', onPress: () => console.log('OK Pressed'), style: 'OK'},
        ]);

      const createTwoButtonAlert = () =>
        Alert.alert('Alert Title', 'My Alert Msg', [
          {
            text: 'Cancel',
            onPress: () => console.log('Cancel Pressed'),
            style: 'cancel',
          },
          {text: 'OK', onPress: () => console.log('OK Pressed')},
        ]);



      return (
        <View style={styles.container}>
          <Button title={'1-Button Alert'} onPress={createOneButtonAlert} />
          <Button title={'2-Button Alert'} onPress={createTwoButtonAlert} />
        
        </View>
      );
    };

    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'space-around',
        alignItems: 'center',
      },
    });

    export default Third;

如何修复我的代码?

【问题讨论】:

    标签: javascript node.js reactjs react-native


    【解决方案1】:

    问题来了

      Alert.alert('Alert Title','//Message required',[
              {text: 'OK', onPress: () => console.log('OK Pressed'), style: 'OK'},
            ]);
    

    如果您看到alert 的调用,第二个参数是字符串消息

    你调用的方式,你传递一个数组而不是预期的字符串,解决这个问题的方法是有一个空消息,或者如果你有一个消息最好放那个。

      Alert.alert('Alert Title','',[
              {text: 'OK', onPress: () => console.log('OK Pressed'), style: 'OK'},
            ]);
    

    当本机代码尝试使用字符串并且传递了一些其他类型(特别是在 android 中)时,会发生此错误“无法从 readablenativemap 转换为字符串”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 2022-11-29
      • 1970-01-01
      相关资源
      最近更新 更多