0

I'm trying to use AlertIOS in a brand new React Native project, but for some reason I'm getting undefined-related errors.

The relevant parts of the code:

import { AlertIOS } from 'react-native';

// Callback for a button press
const _alert = () => {
  AlertIOS.alert('title', 'text')
}

If I'm not in debug mode, after clicking the button I get the error undefined is not an object (evaluating '_reactNative.AlertIOS.alert').

When debugging, the error changes to Cannot read property alert of undefined and AlertIOS has the value shown in the print below, which doesn't seem right.

Also, if I just use Alert.alert, it works (obviously changing the import).

enter image description here

2
  • Are you checking in Ios Device/Simulator because it will not work in Android. According to documentation , We recommend using the Alert.alert method for cross-platform support if you don't need to create iOS-only prompts. Commented Aug 26, 2019 at 3:04
  • @MehranKhan I was testing in the iOS Simulator, but AlertIOS was removed (check the accepted answer). Thanks! Commented Aug 26, 2019 at 15:14

2 Answers 2

2

In RN 0.61, the AlertIOS functionality is now part of Alert itself. So for example, instead of AlertIOS.prompt, you can do the following:

import {Alert} from 'react-native';

Alert.prompt('Alert Title', 'message', [
    {
        text: 'ok',
        onPress: str => console.log('Entered string: ' + str),
    },
    {
        text: 'Cancel',
        onPress: () => console.log('Pressed Cancel!'),
        style: 'cancel',
    },
]);

See this issue for details

Sign up to request clarification or add additional context in comments.

Comments

1

I faced the same problem in RN 0.60. Seem like it's removed. You can check its source https://github.com/facebook/react-native/blob/master/Libraries/react-native/react-native-implementation.js#L50

There are some good alternatives like this one

1 Comment

Yeah, I just found the commit that removed it, weird that the official documentation isn't updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.