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: docs/Style.md
+13-7Lines changed: 13 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,11 @@ permalink: docs/style.html
7
7
next: nativemodulesios
8
8
---
9
9
10
-
## Declaring Styles
10
+
React Native doesn't implement CSS but instead relies on JavaScript to let you style your application. This has been a controversial decision and you can read through those slides for the rationale behind it.
The way to declare styles in React Native is the following:
13
17
@@ -29,6 +33,8 @@ var styles = StyleSheet.create({
29
33
30
34
`StyleSheet.create` construct is optional but provides some key advantages. It ensures that the values are **immutable** and **opaque** by transforming them into plain numbers that reference an internal table. By putting it at the end of the file, you also ensure that they are only created once for the application and not on every render.
31
35
36
+
All the attribute names and values are a subset of what works on the web. For layout, React Native implements [Flexbox](/react-native/docs/flexbox.html).
37
+
32
38
## Using Styles
33
39
34
40
All the core components accept a style attribute
@@ -41,20 +47,20 @@ All the core components accept a style attribute
41
47
and also accepts an array of styles
42
48
43
49
```javascript
44
-
<View style={[style.base, style.background]} />
50
+
<View style={[styles.base, styles.background]} />
45
51
```
46
52
47
53
The behavior is the same as `Object.assign`: in case of conflicting values, the one from the right-most element will have precedence and falsy values like `false`, `undefined` and `null` will be ignored. A common pattern is to conditionally add a style based on some condition.
Finally, if you really have to, you can also create style objects in render, but they are highly discouraged. Put them last in the array definition.
54
60
55
61
```javascript
56
62
<View
57
-
style={[style.base, {
63
+
style={[styles.base, {
58
64
width:this.state.width,
59
65
height:this.state.width*this.state.aspectRatio
60
66
}]}
@@ -63,13 +69,13 @@ Finally, if you really have to, you can also create style objects in render, but
63
69
64
70
## Pass Styles Around
65
71
66
-
In order to let a call site customize the style of your component children, you can pass styles around. Use `View.stylePropType` and `Text.stylePropType` in order to make sure only styles are being passed.
72
+
In order to let a call site customize the style of your component children, you can pass styles around. Use `View.propTypes.style` and `Text.propTypes.style` in order to make sure only styles are being passed.
0 commit comments