To use DraggableGrid, provide a data array where each item has a unique identifier, a numColumns value, and a renderItem function. To ensure the grid updates correctly after a user finishes dragging, you should update your component's state with the new data returned by the onDragRelease event. This allows the component to reconcile the new order with its internal cache.
import React from 'react';
import { View, StyleSheet, Text } from 'react-native';
import { DraggableGrid } from 'react-native-draggable-grid';
export class MyTest extends React.Component {
constructor(props) {
super(props);
this.state = {
data: [
{ name: '1', key: 'one' },
{ name: '2', key: 'two' },
// ... more items
],
};
}
render_item = (item) => (
<View style={styles.item} key={item.key}>
<Text style={styles.item_text}>{item.name}</Text>
</View>
);
render() {
return (
<View style={styles.wrapper}>
<DraggableGrid
numColumns={4}
renderItem={this.render_item}
data={this.state.data}
onDragRelease={(newData) => {
this.setState({ data: newData });
}}
/>
</View>
);
}
}