Very Useful Tips In React Native – Part 1

Tram Ho

We want to develop a multiple platforms application with React Native or have been using it for a long time, but I’m sure it’s not always the common commands or tricks you use! In this article, I will list some useful tips that you can use to find them whenever you need or if someone new to React Native will easily synthesize and apply them.

Of course, if you have done well with RN, you will find some very simple commands, but on the position someone is learning, it is also a bit complicated because different from some other languages ​​such as Swift or Android. You have an IDE to support it all you just need to find out and remember. As for RN, you have to do it by command, of course, we need to remember it, there is no other option, right? Continue to the main part of you …

1. Project initialization order

You run in Terminal: react-native init FirstProject

Note: Leave your project in the desired folder (easy to remember, you must cd pathname_foldername_project ) before running the above command. Avoid the case of creating the project and then return to the code after 2 days not knowing where I left it =))

2. Build App

Running in Terminal, this is of course already so from the next part I will not mention this again

After running the command line in section 1 you execute this command: cd FirstProject

  • Android: react-native run-android
  • iOS: react-native run-ios

3. Show device list

Lists the devices on which you can run your application

  • MAC: xcrun simctl list devices
  • Other: emulator -list-avds

Ex: Pixel_2_API_24

4. Autolinking

This is a new change from RN 0.60, before after you want to add a new library to the project you usually use command: react-native link

But now we will use this new method for both iOS and Android platforms:

5. Upgrading React Native

From the above section, please check your RN version by: react-native -v

EX:

react-native-cli: 2.0.1

react-native: 0.60.5

If it is already old, update it using the following command: npm install --save [email protected]

Next: react-native upgrade

You just want an updated package, then use: npm install --save [email protected]

6. Debug

You can enable some of these features by:

  • Android: Ctrl + M
  • iOS: Command + D

Then select the feature you want:

  • Enabling Hot Reloading
  • Enabling Remote Debugging

7. Useful tips

a. Screen size

b. Text Component

Capitalization:

<Text>{this.props.title.toUpperCase()}</Text>

Switch to lowercase;

<Text>{this.props.title.toLowerCase()}</Text>

Max line:

c. Properties

– Default Prop

– Spread Attributes

– Summary of Object values ​​into a new object

You see in the example that is very concise and even if it is written in a long time, it will not add much code, but in this way, it is not so concise. Because its practical application case will be very different.

For example: Usually I will use this method when retrieving data from the API, the task you need to synthesize it into a new object to transfer to another component to use the way the client wants. So the line of code: const { title, size, ...rest } = this.props; the traditional way of extracting and retrieving data would be very long. Thankfully, RN already supports such declaration.

d. UI Tips

– Use multiple styles

** – Draw 1 circle **

– Show image under a customized View

– Adjust the position of the view:

Placing the view in the desired position is an essential task when making the interface, you can apply the following method to adjust your UI more easily.

You need to pay attention to the parameter of the following object: flexDirection It allows you to arrange the view by row and column

Comes with justifyContent which allows you to arrange content by location, detailed example below:

flex-start

center

flex-end

space-around

space-between

You only need to declare the following in the container style as follows:

Note: Replace the value of flexDirection and justifyContent with the parameters above

  • Next you can use alignItems if you need to align the view vertically

flex-start

center

flex-end

stretch

This is the default state you test run the code to know the results offline.

8. Summary

Above are the tips as well as the commands that we often use and not outside the original purpose we mentioned, hope that someone will find useful in the article we share. If you have any interesting ways and useful tips, don’t be afraid to share yourself and others to learn. If there are difficulties in applying you also leave a comment I will support.

Share the news now

Source : Viblo