React Native 0.60+模拟器点击链接打开主机浏览器原理咨询
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
opencommand - On Windows, it uses the
startcommand - 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

