Skip to content

Handle alerts correctly on web in Button example#4946

Merged
Simek merged 6 commits into
react:mainfrom
yash04577:fix/button-alert-web
Jan 4, 2026
Merged

Handle alerts correctly on web in Button example#4946
Simek merged 6 commits into
react:mainfrom
yash04577:fix/button-alert-web

Conversation

@yash04577

Copy link
Copy Markdown
Contributor

Problem

The Button component examples in the docs did not display alerts correctly on web.

  • On web, using Alert.alert() does not show any message.
  • On iOS/Android, Alert.alert() works as expected.
  • Users copying the example from docs would get a button that appears non-functional on web.

Reproduction Steps (Before Fix)

  1. Open the Button documentation: Button.
  2. Click "Press me" in the first Button example on web.
  3. Observe that nothing happens — no alert is displayed.
  4. On iOS/Android simulators or devices, alert works normally.

Old Behavior (Buggy)

  • All onPress handlers used Alert.alert() regardless of platform.
  • On web, pressing the button had no visible effect.

New Behavior (Fixed)

  • Alerts now display correctly on all platforms:

    • Web: uses window.alert()
    • iOS/Android: uses Alert.alert()
  • Users copying the example now see the expected alert immediately when pressing buttons.


Root Cause

  • Alert.alert() is not supported in browser environments.
  • The Button examples did not include a platform check, so web users saw no feedback.

Fix

  • Added a small helper function:
function showAlert(message: string) {
  if (Platform.OS === 'web') {
    window.alert(message);
  } else {
    Alert.alert(message);
  }
}

@meta-cla meta-cla Bot added the CLA Signed label Jan 3, 2026
@netlify

netlify Bot commented Jan 3, 2026

Copy link
Copy Markdown

Deploy Preview for react-native ready!

Name Link
🔨 Latest commit f40f70e
🔍 Latest deploy log https://app.netlify.com/projects/react-native/deploys/695a707a2dd0412718b0f75a
😎 Deploy Preview https://deploy-preview-4946--react-native.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi

@yash04577

Copy link
Copy Markdown
Contributor Author

Hi

Hi 👋

@Simek Simek left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @yash04577, thanks for the correction! 👍

Just two things:

  • can you also update the unversioned docs from /docs folder at root, and versioned docs at least for the latest version (0.83)?
  • can you run lint:examples script from the /website directory to re-format the changed code?

@yash04577

Copy link
Copy Markdown
Contributor Author

Hey @yash04577, thanks for the correction! 👍

Just two things:

  • can you also update the unversioned docs from /docs folder at root, and versioned docs at least for the latest version (0.83)?
  • can you run lint:examples script from the /website directory to re-format the changed code?

Sure

@yash04577

Copy link
Copy Markdown
Contributor Author

Hey @yash04577, thanks for the correction! 👍

Just two things:

  • can you also update the unversioned docs from /docs folder at root, and versioned docs at least for the latest version (0.83)?
  • can you run lint:examples script from the /website directory to re-format the changed code?

Hi @Simek ,

I'm hitting the no-alert linting error in my code example. Since Alert.alert() doesn't show a visual popup on the Web/Snack player, how would you prefer I handle user feedback?

Should I use React state to show text in the UI

Thanks

@Simek

Simek commented Jan 4, 2026

Copy link
Copy Markdown
Collaborator

The best approach might be to disable the problematic linting rule in the examples lint package ESLint config, since falling back to browser native alert for web seems like the proper thing to do.

@yash04577

yash04577 commented Jan 4, 2026

Copy link
Copy Markdown
Contributor Author

@Simek I have done the required changes

@Simek Simek left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the follow up, LGTM! 👍

@Simek Simek merged commit 9ae17b1 into react:main Jan 4, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants