forked from linux-china/linux-china.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjetpack-compose-is-like-reactjs.html
289 lines (262 loc) · 9.67 KB
/
jetpack-compose-is-like-reactjs.html
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
280
281
282
283
284
285
286
287
288
289
<!DOCTYPE html><html><head><title>Jetpack Compose is like React.js</title><meta charset="utf-8"><style>body {
font-family: "Helvetica Neue", sans-serif;
margin: 0;
}
body * {
box-sizing: border-box;
}
.section {
background-color: #f4f4f4;
padding: 4em 3em;
}
.section:nth-child(2n) {
background-color: white;
}
.title {
font-size: 2.7em;
color: #888;
font-weight: 100;
letter-spacing: 0.2em;
text-align: center;
}
.case {}
.name {
font-size: 1.8em;
color: #444;
font-weight: 300;
text-align: center;
margin: 60px;
}
.pair {
display: flex;
display: -webkit-flex;
display: -ms-flexbox;
justify-content: center;
-webkit-justify-content: center;
-ms-box-pack: center;
-ms-box-align: center;
}
.card {
flex: 1;
-webkit-flex: 1;
-ms-flex: 1;
max-width: 590px;
margin: 0 10px;
}
.lang {
font-size: 1.3em;
color: #666;
padding-bottom: 0;
font-weight: 200;
letter-spacing: 0.07em;
}
.code {
font-size: 1.15em;
background-color: white;
margin: 0.4em 0 0 0;
padding: 0.4em;
line-height: 1.3em;
}
.section:nth-child(2n) .code {
background-color: #f4f4f4;
}
#fork-me {
position: absolute;
right: 0;
}
#note {
font-size: 1.5em;
color: #fff;
text-align: center;
padding: 0.6em;
background: #414141;
font-weight: 300;
letter-spacing: 0.05em;
}
</style><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.1/styles/github.min.css"><script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.1/highlight.min.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body><a target="_blank" href="https://github.com/linux-china/jetpack-compose-is-like-reactjs"><img id="fork-me" src="http://nilhcem.github.io/swift-is-like-kotlin/fork-me.png"></a><div id="note">Jetpack Compose is like React.js</div><div class="section"><div class="title">BASICS</div><div class="case"><div class="name">Hello World</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code>class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Text("Hello world!")
}
}
}
</code></pre></div><div class="card"><div class="lang">TypeScript</div><pre class="code"><code>const element = <h1>Hello, React.js</h1>;
ReactDOM.render(
element,
document.getElementById('root')
);
</code></pre></div></div></div><div class="case"><div class="name">Component</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code>@Composable
fun Greeting(name: String) {
Text (text = "Hello $name!")
}
</code></pre></div><div class="card"><div class="lang">TypeScript</div><pre class="code"><code>function Greeting({name}) {
return <p>Hello {name}</p>
}
</code></pre></div></div></div><div class="case"><div class="name">List</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code>@Composable
fun MessageList(messages: List<Message>) {
Column {
messages.forEach { message ->
MessageRow(message)
}
}
}
</code></pre></div><div class="card"><div class="lang">TypeScript</div><pre class="code"><code>function TodoList({items}) {
return <ul>
{items.map(item =>
<TodoItem item={item} key={item.key}/>)}
</ul>
}
</code></pre></div></div></div><div class="case"><div class="name">State</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code>@Composable
fun Greeting(name: String) {
var text by remember { mutableStateOf("Hello, $name") }
Column {
Text(text = text)
Button(onClick = {
text = "Hello, Clicked!"
}) {
Text(text = "Click me");
}
}
}
</code></pre></div><div class="card"><div class="lang">TypeScript</div><pre class="code"><code>import {useState} from "react";
export function Hello({name}) {
const [text, setText] = useState(name);
return <div>
<p>{text}</p>
<button onClick={event => {
setText("Hello, Clicked!");
}
}>Click me
</button>
</div>
}
</code></pre></div></div></div><div class="case"><div class="name">State for SubComponent</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code>@Composable
fun Greeting(name: String) {
var text by remember { mutableStateOf("Hello, $name") }
Column {
Text(text = text)
Welcome {
text = it
}
}
}
@Composable
fun Welcome(setText: (text: String) -> Unit) {
}
</code></pre></div><div class="card"><div class="lang">TypeScript</div><pre class="code"><code>import {useState} from "react";
export function Hello() {
const [year, setYear] = useState(2017);
return <div>Year: {year} <Welcome year={year} setYear={setYear}/></div>
}
export function Welcome({year, setYear}) {
return <button onClick={event => {
setYear(year + 1);
}
}>{year}</button>
}
</code></pre></div></div></div></div><div class="section"><div class="title">Coroutines/Async</div><div class="case"><div class="name">Coroutines/Promise</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code>// Side-effects in Compose https://developer.android.google.cn/jetpack/compose/side-effects
@Composable
fun MoviesScreen(scaffoldState: ScaffoldState = rememberScaffoldState()) {
val scope = rememberCoroutineScope()
Scaffold(scaffoldState = scaffoldState) {
Column {
Button(
onClick = {
// Create a new coroutine in the event handler to show a snackbar
scope.launch {
scaffoldState.snackbarHostState.showSnackbar("Something happened!")
}
}
) {
Text("Press me")
}
}
}
}
</code></pre></div><div class="card"><div class="lang">TypeScript</div><pre class="code"><code>export function Welcome({year, setYear}) {
return <button onClick={ async event => {
setYear(year + 1);
}
}>{year}</button>
}
</code></pre></div></div></div><div class="case"><div class="name">Side Effect</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code>@Composable
fun LandingScreen(onTimeout: () -> Unit) {
// This will always refer to the latest onTimeout function that
// LandingScreen was recomposed with
val currentOnTimeout by rememberUpdatedState(onTimeout)
// Create an effect that matches the lifecycle of LandingScreen.
// If LandingScreen recomposes, the delay shouldn't start again.
LaunchedEffect(true) {
delay(SplashWaitTimeMillis)
currentOnTimeout()
}
/* Landing screen content */
}
</code></pre></div><div class="card"><div class="lang">TypeScript</div><pre class="code"><code>import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
// Similar to componentDidMount and componentDidUpdate:
useEffect(() => {
// Update the document title using the browser API
document.title = `You clicked ${count} times`;
}, [count]);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
</code></pre></div></div></div></div><div class="section"><div class="title">Material UI</div><div class="case"><div class="name">Button</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code>// https://material.io/
setContent {
MaterialTheme {
Text(
text = "Hello theming",
color = MaterialTheme.colors.primary
)
}
}
</code></pre></div><div class="card"><div class="lang">jsx</div><pre class="code"><code>// https://material-ui.com/
import React from 'react';
import ReactDOM from 'react-dom';
import Button from '@material-ui/core/Button';
function App() {
return (
<Button variant="contained" color="primary">
你好,世界
</Button>
);
}
ReactDOM.render(<App />, document.querySelector('#app'));
</code></pre></div></div></div></div><div class="section"><div class="title">Layout</div><div class="case"><div class="name">Box</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code>@Composable
fun MatchParentSizeComposable() {
Box {
Spacer(Modifier.matchParentSize().background(Color.LightGray))
ArtistCard()
}
}
</code></pre></div><div class="card"><div class="lang">xml</div><pre class="code"><code><Box component="span" m={1}>
<Button />
</Box>
</code></pre></div></div></div><div class="case"><div class="name">Grid/Column</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code>@Composable
fun ArtistCard() {
Column {
Text("Alfred Sisley")
Text("3 minutes ago")
}
}
</code></pre></div><div class="card"><div class="lang">xml</div><pre class="code"><code><Grid container spacing={1}>
<Grid container item xs={12} spacing={3}>
<FormRow />
</Grid>
<Grid container item xs={12} spacing={3}>
<FormRow />
</Grid>
<Grid container item xs={12} spacing={3}>
<FormRow />
</Grid>
</Grid>
</code></pre></div></div></div></div></body></html>