Skip to content

Commit f4d7723

Browse files
authored
2.6.1 #206
2 parents 73576c1 + 9c0b685 commit f4d7723

File tree

15 files changed

+452
-367
lines changed

15 files changed

+452
-367
lines changed

README.md

Lines changed: 89 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -182,44 +182,48 @@ in [Editable Complex Components](#editable-complex-components).
182182
![demo-3](./.github/large-network-example.gif)
183183

184184
```jsx
185-
state = {
186-
loading: true,
187-
users: [],
188-
}
189-
190-
async componentWillMount() {
191-
const response = await fetch('https://randomuser.me/api/?results=5000')
192-
const data = await response.json()
193-
194-
this.setState({
195-
loading: false,
196-
users: data.results.map(a => ({
197-
name: `${a.name.first} ${a.name.last}`,
198-
id: a.registered,
199-
})),
200-
})
201-
}
185+
() => {
186+
const [loading, setLoading] = useState(true);
187+
const [users, setUsers] = useState([]);
188+
189+
useEffect(() => {
190+
const getUsers = async () => {
191+
const response = await fetch('https://randomuser.me/api/?results=5000');
192+
const data = await response.json();
193+
194+
setLoading(false);
195+
setUsers(
196+
data.results.map(a => ({
197+
name: `${a.name.first} ${a.name.last}`,
198+
id: a.registered,
199+
}))
200+
);
201+
};
202+
203+
getUsers();
204+
}, []);
202205

203-
render() {
204206
return (
205207
<View style={{ flex: 1 }}>
206208
<Text style={styles.title}>
207-
{this.state.loading ? 'Fetching' : 'Fetched'} 5000 users
209+
{loading ? 'Fetching' : 'Fetched'} 5000 users
208210
</Text>
209211

210-
{this.state.loading && <ActivityIndicator />}
212+
{loading && <ActivityIndicator />}
211213

212214
<TableView
213215
style={{ flex: 1 }}
214216
tableViewCellStyle={TableView.Consts.CellStyle.Subtitle}
215217
>
216218
<Section>
217-
{this.state.users.map(a => <Item key={a.id}>{a.name}</Item>)}
219+
{users.map(a => (
220+
<Item key={a.id}>{a.name}</Item>
221+
))}
218222
</Section>
219223
</TableView>
220224
</View>
221-
)
222-
}
225+
);
226+
};
223227
```
224228

225229
### App-bundled JSON with filter and selected value checkmarked
@@ -256,7 +260,7 @@ render() {
256260
<View style={{ flex: 1 }}>
257261
<TableView
258262
style={{ flex: 1 }}
259-
editing={this.props.navigation.state.params.editing}
263+
editing={navigation.getParam('editing')}
260264
>
261265
<Section canMove canEdit>
262266
<Item canEdit={false}>Item 1</Item>
@@ -279,59 +283,84 @@ render() {
279283
![pull to refresh example](./.github/pull-to-refresh-example.gif)
280284

281285
```jsx
282-
state = {
283-
loading: true,
284-
users: [],
285-
refreshing: false,
286-
amount: 10,
286+
function reducer(state, action) {
287+
switch (action.type) {
288+
case 'getUsers':
289+
return { ...state, loading: false, users: action.payload };
290+
case 'startRefresh':
291+
return { ...state, refreshing: true };
292+
case 'endRefresh':
293+
return {
294+
...state,
295+
refreshing: false,
296+
amount: state.amount + 10,
297+
users: [...state.users, ...action.payload],
298+
};
299+
default:
300+
return state;
301+
}
287302
}
288303

289-
async componentWillMount() {
290-
const users = await this.fetchUsers()
304+
() => {
305+
const [{ loading, amount, refreshing, users }, dispatch] = useReducer(
306+
reducer,
307+
{
308+
loading: true,
309+
users: [],
310+
refreshing: false,
311+
amount: 10,
312+
}
313+
);
291314

292-
this.setState({
293-
loading: false,
294-
users,
295-
})
296-
}
315+
useEffect(() => {
316+
const getUsers = async () => {
317+
const data = await fetchUsers();
318+
dispatch({ type: 'getUsers', payload: data });
319+
};
297320

298-
fetchUsers = async () => {
299-
const response = await fetch('https://randomuser.me/api/?results=10')
300-
const data = await response.json()
321+
getUsers();
322+
}, []);
301323

302-
return data.results.map(a => ({
303-
name: `${a.name.first} ${a.name.last}`,
304-
id: a.registered,
305-
}))
306-
}
324+
const fetchUsers = async () => {
325+
const response = await fetch('https://randomuser.me/api/?results=10');
326+
const data = await response.json();
307327

308-
fetchMore = () => {
309-
this.setState({ refreshing: true }, async () => {
310-
const users = await this.fetchUsers()
311-
this.setState({ users: [...users, ...this.state.users], refreshing: false, amount: this.state.amount + 10 })
312-
})
313-
}
328+
return data.results.map(a => ({
329+
name: `${a.name.first} ${a.name.last}`,
330+
id: a.login.uuid,
331+
}));
332+
};
333+
334+
const fetchMore = async () => {
335+
dispatch({ type: 'startRefresh' });
336+
const data = await fetchUsers();
337+
dispatch({ type: 'endRefresh', payload: data });
338+
};
314339

315-
render() {
316340
return (
317341
<View style={{ flex: 1 }}>
318342
<Text style={styles.title}>
319-
{this.state.loading ? 'Fetching' : 'Fetched'} {this.state.amount} users
343+
{loading ? 'Fetching' : 'Fetched'} {amount} users
320344
</Text>
321345

322-
{this.state.loading && <ActivityIndicator />}
346+
{loading && <ActivityIndicator />}
323347

324348
<TableView
325349
style={{ flex: 1 }}
326350
tableViewCellStyle={TableView.Consts.CellStyle.Subtitle}
327351
canRefresh
328-
refreshing={this.state.refreshing}
329-
onRefresh={this.fetchMore}
352+
refreshing={refreshing}
353+
onRefresh={fetchMore}
330354
>
331-
<Section>{this.state.users.map(a => <Item key={a.id}>{a.name}</Item>)}</Section>
355+
<Section>
356+
{users.map(a => (
357+
<Item key={a.id}>{a.name}</Item>
358+
))}
359+
</Section>
332360
</TableView>
333361
</View>
334-
)
362+
);
363+
};
335364
}
336365
```
337366

@@ -395,14 +424,14 @@ An `image` prop can be a string pointing to the name of an asset in your "Asset
395424
Catalog". In this case an `imageWidth` prop is recommended.
396425

397426
```jsx
398-
<Item image="icon-success.png" imageWidth={40} />;
427+
<Item image="icon-success.png" imageWidth={40} />
399428
```
400429

401430
Alternatively, you can `require` the image from your local app code. In this case
402431
an `imageWidth` is unnecessary.
403432

404433
```jsx
405-
<Item image={require('../images/icon-success.png')} />;
434+
<Item image={require('../images/icon-success.png')} />
406435
```
407436

408437
### Editable Complex Components
@@ -492,7 +521,7 @@ For more examples, see examples/TableViewDemo.
492521
/>
493522
);
494523
})}
495-
</Section>;
524+
</Section>
496525
```
497526

498527
Note that the props you pass must be primitive types: they cannot be objects.

example/android/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ android {
138138
}
139139

140140
dependencies {
141+
implementation project(':react-native-reanimated')
141142
implementation project(':react-native-gesture-handler')
142143
implementation fileTree(dir: "libs", include: ["*.jar"])
143144
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"

example/android/app/src/main/java/com/tableviewdemo/MainActivity.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.tableviewdemo;
22

33
import com.facebook.react.ReactActivity;
4+
import com.facebook.react.ReactActivityDelegate;
5+
import com.facebook.react.ReactRootView;
6+
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
47

58
public class MainActivity extends ReactActivity {
69

@@ -12,4 +15,14 @@ public class MainActivity extends ReactActivity {
1215
protected String getMainComponentName() {
1316
return "TableViewDemo";
1417
}
18+
19+
@Override
20+
protected ReactActivityDelegate createReactActivityDelegate() {
21+
return new ReactActivityDelegate(this, getMainComponentName()) {
22+
@Override
23+
protected ReactRootView createRootView() {
24+
return new RNGestureHandlerEnabledRootView(MainActivity.this);
25+
}
26+
};
27+
}
1528
}

example/android/app/src/main/java/com/tableviewdemo/MainApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.app.Application;
44

55
import com.facebook.react.ReactApplication;
6+
import com.swmansion.reanimated.ReanimatedPackage;
67
import com.swmansion.gesturehandler.react.RNGestureHandlerPackage;
78
import com.facebook.react.ReactNativeHost;
89
import com.facebook.react.ReactPackage;
@@ -24,6 +25,7 @@ public boolean getUseDeveloperSupport() {
2425
protected List<ReactPackage> getPackages() {
2526
return Arrays.<ReactPackage>asList(
2627
new MainReactPackage(),
28+
new ReanimatedPackage(),
2729
new RNGestureHandlerPackage()
2830
);
2931
}

example/android/settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
rootProject.name = 'TableViewDemo'
2+
include ':react-native-reanimated'
3+
project(':react-native-reanimated').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-reanimated/android')
24
include ':react-native-gesture-handler'
35
project(':react-native-gesture-handler').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-gesture-handler/android')
46

0 commit comments

Comments
 (0)