How to do logging in React Native?

2019-01-10 02:38发布

How can I log a variable in React Native, like using console.log when developing for web?

25条回答
神经病院院长
2楼-- · 2019-01-10 02:46

Just console.log('debug');

And run it you can see the log in the terminal/cd prompt .

查看更多
Deceive 欺骗
3楼-- · 2019-01-10 02:47

Something that just came out about a month ago is "Create React Native App," an awesome boilerplate that allows you (with minimal effort) to show what your app looks like live on your mobile device (ANY with a camera) using the Expo app. It not only has live updates, but it will allow you to see the console logs in your terminal just like when developing for the web, rather than having to use the browser like we did with React Native before.

You would, of course, have to make a new project with that boilerplate... but if you need to migrate your files over, that shouldn't be a problem. Give it a shot.

Edit: Actually it doesn't even require a camera. I said that for scanning a QR code, but you can also type out something to sync it up with your server, not just a QR code.

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-10 02:47

If you are on osx and using an emulator, you can view your console.logs directly in safari web inspector.

Safari => Development => Simulator - [your simulator version here] => JSContext

查看更多
爷、活的狠高调
5楼-- · 2019-01-10 02:49

I prefer to recommend you guys using React Native Debugger. You can download and install it by using this command.

brew update && brew cask install react-native-debugger

or

Just check the link below.

https://github.com/jhen0409/react-native-debugger

Happy Hacking!

查看更多
孤傲高冷的网名
6楼-- · 2019-01-10 02:49

You can do this in 2 methods

1> by using warn

console.warn("somthing " +this.state.Some_Sates_of_variables);

2> By using Alert This is not good each times if it reaches alert then each time pop will be opened so if doing looping means not preferable to use this

Import the {Alert} from 'react-native'
   // use this alert
   Alert.alert("somthing " +this.state.Some_Sates_of_variables);
查看更多
男人必须洒脱
7楼-- · 2019-01-10 02:50

Visual Studio Code has a decent debug console that can show your console.log.

enter image description here

VS Code is, more often than not, React Native friendly.

查看更多
登录 后发表回答