SHISME / react-native-draggable-grid

A draggable and sortable grid of react-native
321 stars 94 forks source link

can't perform drag and drop with function components #40

Open shadow921677 opened 4 years ago

shadow921677 commented 4 years ago

when I use the function component instead of the component classes I can't perform the drap and drop or any other event, the components are displayed but no action is possible.

DEVfancybear commented 4 years ago

+1

samuelcelko commented 3 years ago

+1 any update on this?

toptechdevo commented 3 years ago

Did you long press the item?

bjornlll commented 3 years ago

I'm having a similar issue: Unable to drag certain items. Will post here again as I learn more. I did try with a component generated from styled.View - that seemed to work.

bjornlll commented 3 years ago

Managed to find a workaround for my own use-case. Class vs. functional components is not what made it break in my case.

Didn't work

const renderItem = (item) => {
  return <CompositeComponent {...item} /> 
}

const CompositeComponent = () => <View><SubComponent>...</SubComponent></View>

Works

const renderItem = (item) => {
  return <View><SubComponent>...</SubComponent></View>;
}

In short: When I copy-pasted my component into the renderItem() function it worked. Incl. with functional sub-components.

lreardon commented 3 years ago

Encountering the same issue. @bjornlll's solution seems to be valid, but it's untenable in practice.

talktosalvador commented 2 years ago

properly declare the useState

  const [state, setState] = useState({
    data: [
      {item: 'some', key: 'one'},
      {item: 'like', key: 'two'},
      {item: 'this', key: 'three'},
    ],
  });

properly declare the render_item

const render_item = ({item, key}) => {
  return (
    <View key={key}>
      <Text style={styles.item_text}> {item} </Text>
    </View>
  );
};

properly use the useState

<DraggableGrid
          numColumns={3}
          renderItem={render_item}
          data={state.data}
          onDragRelease={data => {
            setState({...state, data});
          }}
          itemHeight={buttonsSize}
/>
amirbhz86 commented 1 year ago

add this prop

activeOpacity={1}

if you have any TouchableOpacity component in your renderItem

itrend commented 9 months ago

In addition to @bjornlll's workaround, I found out wrapping my single component in a <View> works as well, i.e.

didn't work

const renderItem = (item) => {
  return <CompositeComponent {...item} /> 
}

worked

const renderItem = (item) => {
  return <View><CompositeComponent {...item} /> </View>
}

I'm pretty much showing the same code as @bjornlll, the difference is I didn't copy paste my component in renderItem, I wrapped my component in <View> instead. Hope this is useful to someone.