react-native: hot-loading on two devices simultaneously in development - react-native

I'm developing an app for both Android and iOS at the same time and I have my two phones in front of me. I have hot-loading enabled on both of them, but it seems only the last phone I touched will hot-load and the other one stays inactive until I shake it and choose "reload js" again.
Is this intended behaviour?

I ran into this issue too.
You can get around it by running the packager on two separate ports. In my package.json I have:
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start ",
"port-8082": "node node_modules/react-native/local-cli/cli.js start --port 8082",
},
And in my iOS startup code in AppDelegate I changed the port to 8082 e.g.
jsCodeLocation = [NSURL URLWithString:#"http://localhost:8082/index.ios.bundle?platform=ios&dev=true"];
So then I run npm start in one terminal session (which starts on the default port 8081), and npm run port-8082 in another session. Then 8081 will serve the Android device and 8082 the iOS device, and you can use Hot Reloading on both devices simultaneously.
Edit: With the change of react-native file architecture, the AppDelegate code is now:
jsCodeLocation = [NSURL URLWithString:#"http://localhost:8082/index.bundle?platform=ios&dev=true"];

I use this method to debug both Android and iOS platforms at the same time:
Add ["port-8082": "react-native start --port 8082"] in package.json under "scripts" section to launch another metro process to support Android platform. (You could input [npm run port-8082] to launch this metro process)
Using [adb -s <DeviceName> reverse tcp:8081 tcp:8082] to make Android device(port:8081) mapping to MacOS(port:8082).(You could use [adb devices] to find the <DeviceName> of Android Device)

Related

Metro bundler link 19000 shows json dump on react native expo instead of bundler web page with qr code

I usually run expo start or expo start -c and a browser will auto open at localhost:19000 with the metro bundler details that displays the qr code, connection: tunnel, lan, local, and of course the exp: address of the app.
Suddenly, when I run expo start, the browser no longer open. and when I navigate to localhost:19000, it shows a json dump:
{"name":"litplans","slug":"litplans","version":"1.0.0","orientation":"portrait","icon":"./assets/icon.png","userInterfaceStyle":"light","splash":{"image":"./assets/splash.png","resizeMode":"contain","backgroundColor":"#ffffff","imageUrl":"http://127.0.0.1:19000/assets/./assets/splash.png"},"updates":{"fallbackToCacheTimeout":0},"assetBundlePatterns":["**/*"],"ios":{"supportsTablet":true,"bundleIdentifier":"com.mallsecinc.litplans"},"android":{"adaptiveIcon":{"foregroundImage":"./assets/adaptive-icon.png","backgroundColor":"#FFFFFF","foregroundImageUrl":"http://127.0.0.1:19000/assets/./assets/adaptive-icon.png"}},"web":{"favicon":"./assets/favicon.png"},"_internal":{"isDebug":false,"projectRoot":"C:\\sites\\apps\\litplans","dynamicConfigPath":null,"staticConfigPath":"C:\\sites\\apps\\litplans\\app.json","packageJsonPath":"C:\\sites\\apps\\litplans\\package.json"},"sdkVersion":"45.0.0","platforms":["ios","android","web"],"developer":{"tool":"expo-cli","projectRoot":"C:\\sites\\apps\\litplans"},"packagerOpts":{"scheme":null,"hostType":"lan","lanType":"ip","devClient":false,"dev":true,"minify":false,"urlRandomness":"2x-kif","https":false},"mainModuleName":"node_modules\\expo\\AppEntry","__flipperHack":"React Native packager is running","debuggerHost":"127.0.0.1:19000","logUrl":"http://127.0.0.1:19000/logs","hostUri":"127.0.0.1:19000","bundleUrl":"http://127.0.0.1:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&strict=false&minify=false","iconUrl":"http://127.0.0.1:19000/assets/./assets/icon.png"}
The only thing that I did different is uninstall and installing node.js.
To trouble shoot, I've done the following:
Try to run an exisitng expo project.
List item
start a brand new expo project.
remove node_modules and install again
remove expo module and install again
Metro Bundler UI is no longer available after expo-cli#6.0.0
https://blog.expo.dev/sunsetting-the-web-ui-for-expo-cli-ab12936d2206
For me using "start": "expo start" instead of "start": "expo start --dev-client" worked maybe if you share what your dependencies look like it would be helpful

Task :app:processDebugResources FAILED - REACT-NATIVE - ANDROID-STUDIO

im trying to run the app in my real device using android studio, eveything is ready and correct, but when i run react-native run-android it will first says connect to the server but then this error appears:
want to use the device as debugger
For running expo apps, you would normally do this using the command line.
Navigate to the root of your project, then (if you still have all the defaults set) you should be able to open a terminal in that directory and type npm run start. This will start the metro bundler and the other react native tools.
The first time doing this might take a while, but when it's done, you should see a QR code. Scan it with your device, you should be prompted to download the expo app and then your application will run on your phone. You only need to download the expo app once, and all the apps you develop using expo will run on it.
I'm not totally sure if this answers your question, but I hope this helped.
*note that this assumes that you have and use npm, the process is slightly different for yarn and other package managers. If npm run start doesn't work for you, make sure you have npm in the PATH variable, and check the package.json file. You should see an entry in scripts - "start": "expo start". You need to type npm run then whatever your key is for the script that has a value of "expo start"

open android simulator with React Native by default

Is there any way to open react native app and simulator using one command..
i mean some thing like : react-native run-android --emulator 'PIXEL_API_27'
i also try :
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"android": "react-native run-android --emulator 'PIXEL_API_27'",
"test": "jest"
},
and run command npm run android but its complaining that error: unknown option--simulator'`
i am very new with react native. pls help me ....
There is currently no way to open a terminal by passing additional flags to react-native run-android.
So your problem is two fold. First how to run the emulator from the command line, and second how to chain that command with running your react-native project. Let's deal with them in that order.
Opening the emulator from the command line
It is possible to open an emulator via the command line. that is done with the emulator #avd command, replacing #avd with the name of your android virtual device. For example:
emulator #Nexus_5X_API_27_x86
To get the names of the installed android virtual devices on your machine run emulator -list-avds.
Now that your emulator is running let's get react-native running too.
Running the emulator and then running react-native run-android
I have found one issue with running the emulator from the command line, the emulator command keeps executing meaning that you cannot chain commands using &&. However you can run the emulator terminal in the background, by using & so we can chain the commands in the following way:
emulator #Nexus_5X_API_27_x86 & react-native run-android
If you are happy running the terminal in the background, this could be the solution for you.
For more settings for running the emulator from the command line see the docs
Be warned you may get errors when you try to run the emulator command. The most common that I have come across is the PANIC: Missing emulator engine program for 'x86' CPUS. This may require you to dive into your .bashrc or your .bash_profile to get the emulator command to behave. You can see more here

React native stuck at loading from localhost : 8081 on physical device using USB debugger

When I run react-native run-android it builds successfully and there after
Mobile screen shows nothing more than loading from local host : 8081
I have tried adb reverse tcp:8081 tcp:8081
Start by closing the App on your phone and clearing phone memory.
Close metro bundler on your PC.
Then clear npm cache as follows: npm cache clean --force
Then clean up gradle as follows:
Change into android directory in your project folder: cd android
To clean clean gradle simply run: ./gradlew clean
cd .. back into your project's root directory.
Build your app again using whichever method suits you. My preferred method for RN apps without expo on a windows machine is as follows:
npx react-native run-android
Try either of these ways to fix it:
Try to Open Developer menu by press Ctrl + M in emulator and choose the Setting port, then input the value localhost:8081.
Try to connect the other wifi, then run 'adb reverse tcp:8081 tcp:8081'
Try to change the URL on chrome to http://localhost:8081/debugger-ui/
Cheer!
Samsung DeX!
Samsung DeX runs in the background to discover device connections, it uses port 8081. In my case, it interfered with Metro and adb reverse. Exiting the system tray app fixed the entire issue.
For some reason it was a watchman issue. I remember installing watchman a few days before and it was causing the issue. I uninstalled watchman and it worked.
Uninstalling watchman made it work for me.

Why I am not able to run react-native application on my android device?

I am getting this error when I try to run react-native application on my device.
Steps I did:
react-native init SampleApp
cd SampleApp
react-native run-android
react-native start
PS: On first attempt I did successfully run react-native app. Now I am getting this error. I searched a lot but couldn't find solution yet.
Normally, running react-native run-android will open up another command prompt to start the React Packager.
If the packager is somehow not started properly, you can try the following steps:
Run react-native start first to start the packager server manually.
Run react-native run-android which it will detect the packager is running and proceed with deploying the application into the device/emulator.
First, you mus open the react-native local packet server:
react-native start or npm start.
If there is still a problem, may be:
The IP address might be wrong.
Phone cannot access the IP address
Shake your devices show Menu(or click your devices Menu), click the last item dev config setup your computer ip, like 192.168.*.*:8081
The reason is that another application takes port 8081, which is needed for running Metro Bundler. After doing some research, I found out someone does "react-native start" to start the bundler before running "react-native run-ios/android". Running "react-native run-ios" is successful when I previously started the Bundler. After running "react-native run-android", the app can be viewed. However, after could click R to reload the app, it shows 404.
Make sure your port 8081 is not taken by another app. If so, every time when we type "react-native run-ios/android", the Bundler can be automatically started.
When the app is loaded, the window of Bundler should have a progress bar showing the detail of loading the bundle file under ios/android.
BUNDLE [android, dev] ./index.js ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ 100.0% (481/481), done.