Stying in React Native is not the same as normal CSS. For styling elements in React Native, JavaScript objects are used. Every core component in React Native accepts the style prop which accepts a JavaScript object containing CSS property names as key. Any CSS property used in these objects follows the camel case convention, for instance. backgroundColor is used rather than background-color.
Creating ReactNative App and Module Installation:
Step 1: Open your terminal and install expo-cli by the following command.
npm install -g expo-cli
Step 2: Now create a project by the following command.
expo init demo-app
Step 3: Now go into your project folder i.e. demo-app
cd demo-app
Project Structure: It will look like the following.
Approach 1: Using Inline Styles: Using inline styles is preferred only for small very small components. For large-scale production applications, it becomes cumbersome and inefficient to use inline styles.
- App.js
Start the server by using the following command.
npm run android
Output: If your emulator did not open automatically then you need to do it manually. First, go to your android studio and run the emulator. Now start the server again.
Approach 2: Using StyleSheet: As a component grows in complexity, it is much cleaner and efficient to use StyleSheet.create to define several styles in one place.
- App.js
Start the server by using the following command.
npm run android
Output: If your emulator did not open automatically then you need to do it manually. First, go to your android studio and run the emulator. Now start the server again.
Reference: https://reactnative.dev/docs/stylesheet