在 OK 按钮上有 onPress 时,React 本机警报显示错误

React native alert shows error when having an onPress on the OK button

我试图在使用开关时显示警报(atm 仅在 Ios 上),但它显示错误:

JSON value '({text = OK;})' of type NSMutableArray cannot be converted to NSString

这是我使用的代码示例:

import React, { Component } from 'react';
import { StyleSheet,Text,View,Button,TouchableOpacity,TextInput,AsyncStorage,ScrollView, Alert,Switch } from 'react-native';
import RNRestart from 'react-native-restart';

export default class ProfileScreen extends Component {    
    constructor(props) {
        super(props);
        this.state = {
            colorPattern: false
        }
    }

    saveThemeColour(color) {
        if(color) {
            AsyncStorage.setItem('themeColour', 'default');
            this.setState({colorPattern: true});
            Alert.alert(
                I18n.t('restart theme change'),
                [
                    {text: 'OK', onPress: () => RNRestart.Restart()}
                ],
                { cancelable: false }
            )
        }else{
            AsyncStorage.setItem('themeColour', 'white');
            this.setState({colorPattern: false});
        }
    }
    <Switch value={this.state.colorPattern} onValueChange={ (value) => this.saveThemeColour(value)}/>
}

当我只留下警报的标题时它工作正常。

您缺少 消息 参数:

Alert.alert(
  I18n.t('restart theme change'),
  <here-comes-the-message-as-string>,
  [
    {text: 'OK', onPress: () => RNRestart.Restart()}
  ],
  { cancelable: false }
)