You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native 0.60+模拟器点击链接打开主机浏览器原理咨询

Why does fetching RN Dev Server's open-url open the host browser instead of the simulator's?

Great question! Let's break down exactly what's happening here step by step:

1. The openURLInBrowser function talks to React Native's own dev server

First, let's look at the target of that fetch call: getDevServer().url + 'open-url' — this points to React Native's local development server (the one running at http://localhost:8081 when you start your app with npm start or yarn start), not a third-party backend API.

2. The dev server has a special handler for the open-url endpoint

When the dev server receives this POST request (with the target URL in the request body), it doesn't send back JSON or data like a typical API would. Instead, it runs a system-level command on your host machine to open the URL:

  • On macOS, it executes the open command
  • On Windows, it uses the start command
  • On Linux, it calls xdg-open

These commands tell your host operating system to launch its default browser and load the provided URL — that's why the link opens on your computer, not the simulator or physical device.

3. Fetch is just an HTTP client (the magic is in the server, not fetch)

You're right that fetch is commonly used to communicate with backend APIs, but at its core, it's just a tool for sending HTTP requests to any server that speaks HTTP. The behavior you're seeing has nothing to do with fetch itself — it's entirely about what the React Native dev server is programmed to do when it receives that specific open-url request.

Think of it this way: if you sent a fetch request to a server set up to send you a cat image, fetch would just deliver that image back. Here, the server is set up to trigger a system action instead of returning data.

内容的提问来源于stack exchange,提问作者Isaac

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:02:35