Skip to content

Commit f20d29b

Browse files
authored
Update README.md
1 parent f8227d7 commit f20d29b

File tree

1 file changed

+51
-52
lines changed

1 file changed

+51
-52
lines changed

README.md

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -59,74 +59,73 @@ Example snack: https://snack.expo.io/@computerjazz/rndfl-example
5959
Example snack with scale effect on hover: https://snack.expo.io/@computerjazz/rndfl-dragwithhovereffect
6060

6161
```typescript
62-
import React, { Component } from "react";
63-
import { View, TouchableOpacity, Text } from "react-native";
64-
import DraggableFlatList, { RenderItemParams } from "react-native-draggable-flatlist";
62+
import React, { useState, useCallback } from 'react';
63+
import { View, TouchableOpacity, Text } from 'react-native';
64+
import DraggableFlatList, {
65+
RenderItemParams,
66+
} from 'react-native-draggable-flatlist';
6567

66-
const NUM_ITEMS = 10
68+
const NUM_ITEMS = 10;
6769

6870
function getColor(i: number) {
6971
const multiplier = 255 / (NUM_ITEMS - 1);
7072
const colorVal = i * multiplier;
7173
return `rgb(${colorVal}, ${Math.abs(128 - colorVal)}, ${255 - colorVal})`;
7274
}
7375

76+
const exampleData: Item[] = [...Array(20)].map((d, index) => {
77+
const backgroundColor = getColor(index);
78+
return {
79+
key: `item-${backgroundColor}`,
80+
label: String(index),
81+
backgroundColor,
82+
};
83+
});
84+
7485
type Item = {
7586
key: string;
7687
label: string;
7788
backgroundColor: string;
78-
}
89+
};
7990

80-
const exampleData: Item[] = [...Array(20)].map((d, index) => {
81-
const backgroundColor = getColor(index)
82-
return {
83-
key: `item-${backgroundColor}`,
84-
label: String(index),
85-
backgroundColor,
86-
}
87-
});
88-
89-
class Example extends Component {
90-
state = {
91-
data: exampleData
92-
};
91+
function Example() {
92+
const [data, setData] = useState(exampleData);
9393

94-
renderItem = ({ item, index, drag, isActive }: RenderItemParams<Item>) => {
95-
return (
96-
<TouchableOpacity
97-
style={{
98-
height: 100,
99-
backgroundColor: isActive ? "red" : item.backgroundColor,
100-
alignItems: "center",
101-
justifyContent: "center"
102-
}}
103-
onLongPress={drag}
104-
>
105-
<Text
94+
const renderItem = useCallback(
95+
({ item, index, drag, isActive }: RenderItemParams<Item>) => {
96+
return (
97+
<TouchableOpacity
10698
style={{
107-
fontWeight: "bold",
108-
color: "white",
109-
fontSize: 32
99+
height: 100,
100+
backgroundColor: isActive ? 'red' : item.backgroundColor,
101+
alignItems: 'center',
102+
justifyContent: 'center',
110103
}}
111-
>
112-
{item.label}
113-
</Text>
114-
</TouchableOpacity>
115-
);
116-
};
117-
118-
render() {
119-
return (
120-
<View style={{ flex: 1 }}>
121-
<DraggableFlatList
122-
data={this.state.data}
123-
renderItem={this.renderItem}
124-
keyExtractor={(item, index) => `draggable-item-${item.key}`}
125-
onDragEnd={({ data }) => this.setState({ data })}
126-
/>
127-
</View>
128-
);
129-
}
104+
onLongPress={drag}>
105+
<Text
106+
style={{
107+
fontWeight: 'bold',
108+
color: 'white',
109+
fontSize: 32,
110+
}}>
111+
{item.label}
112+
</Text>
113+
</TouchableOpacity>
114+
);
115+
},
116+
[]
117+
);
118+
119+
return (
120+
<View style={{ flex: 1 }}>
121+
<DraggableFlatList
122+
data={data}
123+
renderItem={renderItem}
124+
keyExtractor={(item, index) => `draggable-item-${item.key}`}
125+
onDragEnd={({ data }) => setData(data)}
126+
/>
127+
</View>
128+
);
130129
}
131130

132131
export default Example;

0 commit comments

Comments
 (0)