You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 8-styling/8.0-intro.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
# Styling in React Native
2
2
3
-
In react native we use StyleSheets to write our styling. StyleSheets can be thought of something like a subset of CSS along with few additional helpers for RN. StyleSheets unlike CSS are pure JavaScript objects. This might confuse some web developers in the beginning and introduces a slight learning curve. StyleSheets in React Native are used somewhat similar to that of inline styles in web. Hence, you do not have access to pseudoclasses like `:hover`,`:active`, etc.
3
+
In React Native we use StyleSheets to write our styling. StyleSheets can be thought of as a subset of CSS along with a few additional helpers for RN. StyleSheets, unlike CSS, are pure JavaScript objects. This might confuse some web developers in the beginning and introduce a slight learning curve. StyleSheets in React Native are used somewhat similarly to inline styles in web. Hence, you do not have access to pseudo-classes like `:hover`,`:active`, etc.
4
4
5
5
### Example
6
6
7
-
Imagine we want to add padding and border to a span. In CSS we will write:
7
+
To add padding and border to a span in CSS we will write:
8
8
9
9
```css
10
10
.button {
@@ -13,15 +13,15 @@ Imagine we want to add padding and border to a span. In CSS we will write:
13
13
border: 1pxsolidblack;
14
14
}
15
15
```
16
-
and then we will add this class to our span like this:
16
+
Then we will add this class to our span like this:
17
17
```html
18
18
<spanclass="button"> Test </span>
19
19
```
20
20
Resulting in :
21
21
22
22
<spanstyle='padding:15px;border:1pxsolidblack;'> Test </span>
23
23
24
-
In React Native, there is no concept of pixels or classes. Instead, our sizes will be specified in "units" which will then be translated to pixels based on the pixel density of the screen automagically by RN. If we write the same `View` in a React Native StyleSheet, it would look something like this.
24
+
In React Native, there is no concept of pixels or classes. Instead, our sizes will be specified in "units" which will then be translated to pixels based on the pixel density of the screen automatically by RN. If we write the same `View` in a React Native StyleSheet, it would look something like this:
Even though this convention is good and will help you maintain consistency in small,medium and large text sizes across the app,
32
-
it has few fundamental faults:
31
+
Even though this convention is good and will help you maintain consistency in small, medium and large text sizes across the app, it does have a few fundamental flaws:
32
+
33
+
- Business/marketing team may come up with a change in the requirement that the large font size needs to be 18 instead of 16.
33
34
34
-
- Business/Marketing team may come up with a new requirement that now they want the large font size to be 18 instead of 16.
35
35
Now as a developer, you will need to make changes in the entire app and replace every instance of fontSize:16 with fontSize:18, which kind of sucks!.
36
-
- A new developer who joined the team might not be aware of all the conventions followed by the team and may create a component with fontSize other than 12,14 or 16 thus accidently destroying the code standard/convention.
36
+
- A new developer who joins the team might not be aware of all the conventions followed by the team and may create a component with fontSize other than 12,14 or 16, thus accidentaly overlooking the code standard/convention.
37
37
38
38
39
39
Enter **`theme variables`**.
40
40
41
-
In order to solve the abovementioned issues we introduce a common file `theme.style.js` which will be located at `app/styles/theme.style.js`.
41
+
In order to solve the above-mentioned issues, we introduce a common file `theme.style.js` which will be located at `app/styles/theme.style.js`.
42
42
43
43
In the theme file we define our `theme` variables as follows:
44
44
@@ -57,7 +57,7 @@ export default {
57
57
};
58
58
```
59
59
60
-
and we can use it like this
60
+
and we can use them like this:
61
61
62
62
>styles.js
63
63
@@ -84,14 +84,14 @@ Now our theme file dictates the size of fonts and the primary color, etc.
84
84
85
85
This gives us two benefits:
86
86
- If our business team now tell us to change the font sizes, we can change the theme variables at one place and it gets reflected in the entire app.
87
-
- This will enable us to write multiple theme files which inturn adds basic themeing support to our app. For example: we can write two theme files - one for light theme and one for dark theme and give our app users the option to switch the themes.
87
+
- This will enable us to write multiple theme files which in turn adds basic theming support to our app. For example we can write two theme files - one for a light theme and one for a dark theme and give our app users the option to switch between the themes.
88
88
89
89
90
90
### Integrating theme variables into our NoteTaker demo
91
91
92
-
Enough of the theory, lets try and inculcate this concept into our demo app.
92
+
Enough of theory. Let's try and inculcate this concept into our demo app.
93
93
94
-
First of all add the file `app/style/theme.style.js`.
94
+
First of all, add the file `app/style/theme.style.js`.
Copy file name to clipboardExpand all lines: 8-styling/8.2-common-styles-mixins.md
+14-13Lines changed: 14 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
In React Native, each component is styled using inline styles. This means that it becomes slightly tricky to share styles as you can in web.
4
4
5
-
In web we write a class
5
+
In web, we write a class
6
6
7
7
```css
8
8
.btn {
@@ -17,7 +17,7 @@ Now if we want to apply this class to two different divs we will do so as follow
17
17
<divclass='btn second-btn'>Second button</div>
18
18
```
19
19
20
-
Now same is possible in react native as follows:
20
+
The same is possible in React Native in the following way:
21
21
22
22
23
23
**`styles.js`**
@@ -54,13 +54,13 @@ import styles from './styles.js';
54
54
</View>
55
55
```
56
56
57
-
While this solves the problem only if the style objects are in the same component because in RN we do not import styles from other components (each component has its own style). But in web, we could have just reused the class anywhere (since css is global).
57
+
This solves the problem only if the style objects are in the same component because in RN we do not import styles from other components (each component has its own style). But in web, we could have just reused the class anywhere (since css is global).
58
58
59
59
To solve the problem of reusable styles in React Native, we introduce another file named **`app/style/common.style.js`**
60
-
This is where we will write our mixins / common styles.
60
+
This is where we will write our mixins/common styles.
61
61
62
62
63
-
Hence,if all the buttons in our app have similar style we can write a style with the similar properties inside the **common.style.js**
63
+
Hence,if all the buttons in our app have a similar style we can write a style with similar properties inside the **common.style.js**
64
64
65
65
**`app/style/common.style.js`**
66
66
```js
@@ -107,13 +107,13 @@ import styles from './styles.js';
107
107
```
108
108
109
109
This way our mixins/common style file will provide us the base styles which are common across the app and we write component specific styles in the component style file.
110
-
Thus allowing significant style reuse and avoiding code duplication.
110
+
This allows significant style reuse and avoids code duplication.
111
111
112
112
113
113
### Integrating common/mixin styles into our NoteTaker demo
114
114
115
-
Before we go into common styles , lets modify our code a bit to add another component for entering title for our note.
116
-
Modify the following files as follows:
115
+
Before we go into common styles, let's modify our code a bit to add another component for entering a title for our note.
116
+
Modify the files as follows:
117
117
118
118
**`app/components/Home/Home.component.js`**
119
119
@@ -208,11 +208,12 @@ Our app should now look like this:
208
208
<br>
209
209
210
210
211
-
If you notice, even though we have theme file, our style code has lot of duplicated code. Primarily because we are repeating our styling for text input and also for the heading.
211
+
If you notice, even though we have a theme file, our style code has a lot of duplicated code. This is primarily because we are repeating our styling for text input and also for the heading.
212
212
213
-
The solution to this problem as discussed before is common styles/ mixins
213
+
The solution to this problem, as discussed before, is common styles/mixins.
- This makes the component code much cleaner. The style is present in its own separate file.
83
-
- This allows you two write two different style files for android and ios when needed. Thus you keep the same functionality but the button looks different as needed in two different platforms.
83
+
- This allows you to write two different style files for Android and iOS if required. Thus, you can keep the same functionality but the button can look different based on the requirement for the platform.
Thus by simply moving the styles into a separate file we could achieve a style code that behaves exactly the way we needed in different platforms. Also, we could reuse the component logic.
143
+
Thus by simply moving the styles into a separate file, we could achieve a style code that behaves exactly the way we needed on different platforms. Also, we could reuse the component logic.
144
144
145
145
146
146
147
147
### Conclusion
148
148
149
-
In Web, we have lots of production grade tools like Sass, Less, etc which allows us to write modular, scoped CSS which are easier to manage. These tools then take care of building all our style code into one cohesive stylesheet for our entire application. In React Native, we must think of styling in a slightly different manner. By doing some pre-planning and organization before writing the code for the components, we can reduce code duplication and unnecessary confusions. It takes a bit of getting used to, but styling in React Native is as powerful as the CSS for the web and is the fastest way to build multiplatform native applications.
149
+
In Web, we have lots of production grade tools like Sass, Less, etc which allow us to write modular, scoped CSS which is easier to manage. These tools then take care of building all our style code into one cohesive stylesheet for our entire application. In React Native, we must think of styling in a slightly different manner. By doing some pre-planning and organization before writing the code for the components, we can reduce code duplication and unnecessary confusions. It takes a bit of getting used to, but styling in React Native is as powerful as CSS for the web and is the fastest way to build multi-platform native applications.
0 commit comments