How do you debug React Native?

后端 未结 30 2571
无人共我
无人共我 2020-11-27 10:19

How does one debug their React code with React Native while the app is running in app simulator?

相关标签:
30条回答
  • 2020-11-27 10:39

    Having a space in the file path prevents the Cmd+D from working. I moved my project to a location without a space and I finally got the Chrome debugger to work. Seems like a bug.

    0 讨论(0)
  • 2020-11-27 10:40

    Step 1: Place debugger where ever you want to stop script, like:

      async saveItem(item, selectedValue) {
        debugger
        try {
            await AsyncStorage.setItem(item, selectedValue);
        }
        catch (error) {
            console.error('AsyncStorage error: ' + error.message);
        }
    }
    

    This will pause the debugger when ever control comes to this block of code.

    Step 2: Press Cmd+D on ios emulator and Cmd+M on Android simulator. If you are having real device, shake the device to open dev menu, if you don't want to shake device follow this blog

    Step 3: Select Enable Remote JS Debugging, this will open Chrome

    Step 4: Select Developer Tools.

    Step 5: Your debugger is paused in Sources tab wherever you have written debugger within your code . Go to console and type any parameters you want to debug (that are present in the code block) like: To move to next debugger point again move to Sources -> click on Resume script execution button (Right corner blue button)

    Place the debugger, wherever you wanna pause the script.

    Enjoy debugging!!

    0 讨论(0)
  • 2020-11-27 10:41

    Instead of Cmd+M, for Android Emulator Press F10 in Windows. The emulator starts to show all the react-native debug options.

    image

    0 讨论(0)
  • 2020-11-27 10:42

    Cmd+D from within the Simulator. It'll popup Chrome and from there you can use the Developer Tools.

    Edit:

    This is now linked in the help docs.

    0 讨论(0)
  • 2020-11-27 10:42

    If you're using Redux, I highly recommend React Native Debugger. It includes Chrome devtools, but also has Redux devtools and React devtools.

    Redux Devtools: This allows you to view your actions, and step back and forth through them. It also allows you to view your redux store and has a feature to automatically diff the previous state with the updated state for each action, so you can see that as you step back and forth through a series of actions.

    React Devtools: This allows you to inspect a certain component, namely all of it's props as well as it's component state. If you have a piece of the component state which is a boolean, it lets you click it to toggle it and see how your app reacts when it changes. Great feature.

    Chrome Devtools Allows you to see all your console outputs, use breakpoints, pause on debugger; etc. Standard debugging features. If you right click the area where your actions are listed in Redux Devtools and select 'Allow Network Inspect', you can then inspect your API calls in the network tab of Chrome Devtools which is sweet.

    In conclusion having these all in one place is fantastic! If you don't need one of them you can toggle it on/off. Get React Native Debugger and enjoy life.

    0 讨论(0)
  • 2020-11-27 10:43

    To me the best way to debug on React-Native is by using "Reactotron".

    Install Reactotron then add these to your package.json:

    "reactotron-apisauce": "^1.1.2",
    "reactotron-react-native-under-37": "^1.1.2",
    "reactotron-redux": "^1.1.2", 
    

    now, it just the matter of logging in your code. e.g.: console.tron.log('debug')

    0 讨论(0)
提交回复
热议问题