-
Notifications
You must be signed in to change notification settings - Fork 2
/
ReactNative.js
288 lines (237 loc) · 7.43 KB
/
ReactNative.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// REACT NATIVE - Javascript Library for mobile - by Beumsk
/**/
// REACT NATIVE TRANSLATES TO IOS AND ANDROID
// get started with expo
// npx create-expo-app --template
// npx export start
// npm start
// basic example
import React from 'react';
import { Text, View } from 'react-native';
export default function App() {
return (
<View>
<Text>Hello React Native</Text>
</View>
)
}
// ELEMENTS
// basic elements
import React from 'react';
import { Text, View } from 'react-native';
export default function App() {
return (
<View>{/* similar to <div> but non-scrolling */}
<Text>Hello React Native</Text>{/* similar to <p> */}
</View>
)
}
// scroll view
import React from 'react';
import { ScrollView, Text, View } from 'react-native';
export default function App() {
return (
<View>{/* similar to <div> but non-scrolling */}
<ScrollView>{/* similar to <div> with scroll => should be used inside View because Parent will define its size */}
<Text>very long content...</Text>
</ScrollView>
</View>
)
}
// input
import React, { useState } from 'react';
import { Text, TextInput, View } from 'react-native';
export default function App() {
const [input, setInput] = useState('');
return (
<View>
<TextInput placeholder="text" value={input} onChangeText={(e) => setInput(e)} />{/* similar to <input type='text'> */}
<Text>{input}</Text>
</View>
)
}
// image elements
import React from 'react';
import { Image, ImageBackground, Text, View } from 'react-native';
export default function App() {
return (
<View>
<Image source={require('./path-to-img.png')} />{/* similar to <img> */}
<ImageBackground source={require('./path-to-img.png')}>
<Text>...</Text>{/* similar to <Image />, but with children */}
</ImageBackground>
</View>
)
}
// interaction elements
import React from 'react';
import { Button, Pressable, Text, TouchableHighlight, TouchableOpacity, TouchableWithoutFeedback, View } from 'react-native';
export default function App() {
return (
<View>
<Button title="Click me" onPress={functionName} />{/* title and onPress are required */}
<TouchableHighlight onPress={functionName}><Text>Click me</Text></TouchableHighlight>
<TouchableOpacity onPress={functionName}><Text>Click me</Text></TouchableOpacity>
<TouchableWithoutFeedback onPress={functionName}><Text>Click me</Text></TouchableWithoutFeedback>
{/* Pressable is go-to option!! better for style and detects long press */}
<Pressable onPress={functionName} style={({pressed}) => pressed && {opacity: 0.5}}>
<Text>Click me</Text>
</Pressable>
</View>
)
}
// flatlist to optimize long lists
import React from 'react';
import { FlatList, Text, View } from 'react-native';
export default function App() {
const veryLongList = [{text: '', id: ''}];
return (
<View>
{/* keyExtractor is not needed if key is defined in array */}
<FlatList data={veryLongList} renderItem={x => <Text>{x.item.text}</Text>} keyExtractor={x => x.id} />
</View>
)
}
// STYLE
// based on css but not as complete
// cascade doesn't work except for nested <Text><Text></Text></Text>
// inline style
// shorthand css is not possible => border does not work but borderWidth does
import React from 'react';
import { Text, View } from 'react-native';
export default function App() {
return (
<View style={{background: 'red', borderWidth: 1}}>
<Text>Hello React Native</Text>
</View>
)
}
// style with stylesheet
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
const styles = StyleSheet.create({
container: {
// colors can be string, hex or rgb-rgba
backgroundColor: 'red',
// border cannot use shorthand property => use borderWidth and borderColor
borderWidth: 1,
borderColor: 'black'
// border radius does not work on Text in iOS
borderRadius: 4
}
})
export default function App() {
return (
<View style={styles.container}>
<Text>Hello React Native</Text>
</View>
)
}
// everything is flexbox; default direction is column
import React from 'react';
import { Text, View } from 'react-native';
export default function App() {
return (
<View style={{flex: 1}}>
<View style={{flex: 1, background: 'red'}}><Text>1/6</Text></View>
<View style={{flex: 2, background: 'orange'}}><Text>2/6</Text></View>
<View style={{flex: 3, background: 'yellow'}}><Text>3/6</Text></View>
</View>
)
}
// STATUSBAR
import React, { useEffect } from "react";
// import { StatusBar } from 'react-native'; // expo is simpler
import { StatusBar } from 'expo-status-bar';
export default function App() {
// useEffect(() => {
// StatusBar.setBarStyle("light-content"); // or dark-content
// }, []);
return (
<>
{/*<StatusBar />*/}
<StatusBar style="auto" /> {/* or light or dark */}
<View>
<Text>Hello React Native</Text>
</View>
</>
)
}
// SAFE VIEW
// npx expo install react-native-safe-area-context
import { Text, View } from 'react-native';
import { StatusBar } from 'expo-status-bar';
import { SafeAreaView, SafeAreaProvider } from 'react-native-safe-area-context';
export default function App() {
return (
<SafeAreaProvider>
<SafeAreaView style={{ flex: 1 }}>
<StatusBar style="auto" />
<View><Text>Safe text</Text></View>
</SafeAreaView>
</SafeAreaProvider>
);
}
// PLATFORM SPECIFIC
// check OS (platform)
import React from "react";
import { Platform } from 'react-native';
// platform-specific extensions => BigButton.ios.js & BigButton.android.js
import BigButton from './BigButton'; // will import the right one based on platform
function ComponentIOS() {return <Text>Hello IOS</Text>}
function ComponentANDROID() {return <Text>Hello ANDROID</Text>}
export default function App() {
const Component = Platform.select({
ios: () => require('ComponentIOS'),
android: () => require('ComponentANDROID'),
})();
return (
<>
<View>
{Platform.OS === 'ios' ? <ComponentIOS /> : null }
{Platform.OS === 'android' ? <ComponentANDROID /> : null }
<BigButton />
</View>
{/* OR */}
<Component />;
</>
)
}
// Navigation
// need install of react-navigation
// npm install @react-navigation/native @react-navigation/native-stack
// npx expo install react-native-screens react-native-safe-area-context
import * as React from 'react';
import {NavigationContainer} from '@react-navigation/native';
export default function App() {
return (
<NavigationContainer>{/* Rest of your app code */}</NavigationContainer>
);
};
import * as React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
const Stack = createNativeStackNavigator();
const MyStack = () => {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} options={{title: 'Welcome'}} />
<Stack.Screen name="Profile" component={ProfileScreen} />
</Stack.Navigator>
</NavigationContainer>
);
};
const HomeScreen = ({navigation}) => {
return (
<Button
title="Go to Jane's profile"
onPress={() =>
navigation.navigate('Profile', {name: 'Jane'})
}
/>
);
};
const ProfileScreen = ({navigation, route}) => {
return <Text>This is {route.params.name}'s profile</Text>;
};