react-dnd icon indicating copy to clipboard operation
react-dnd copied to clipboard

Wrapped comps fail to render?

Open CNSKnight opened this issue 9 years ago • 6 comments

Has anyone encountered DragSource'd components failing to render? I've built a collection of "items" to be enabled to be dragged onto a target:

const ItemBloc = props => {
    let item = props.item;
    let btn = (
        <Button data-idx={props.idx} key={item.name} name={item.name}>
            <Icon left>{item.icon}</Icon>
            <span>{item.name}</span>
        </Button>
    );
    return props.connectDragSource(btn);
};

const spec = {
    beginDrag(props, monitor) {
        return {name: props.name};
    }
};

const collect = (connect, monitor) => {
    return {
        connectDragSource: connect.dragSource()
    };
};

const ItemsBar = props => {
    let items;
    if (!props.items || !props.itemtions.length) {
        items = <p>No Items to Display</p>;
    } else {
        items = props
            .itemtions
            .map((item, idx) => {
                let iBloc = <ItemBloc item={item} key={item.name} {...props}/>;
                return DragSource('item', spec, collect)(iBloc);
            }, props);
    }

    return <div className="itemsBar">{items}</div>;
}

export default ItemsBar;

Without the DragSource wrapper, items render fine. As above,, itemsBar renders but items do not? (there are no errors)

Additionally, this modification in the DragSource call also does not work as would be expected - ie nothing is rendered:

const itemsBar = props => {
    let items = [];
    if (!props.items || !props.items.length) {
        items.push(
            <p>No itemtions to Display</p>
        );
    } else {
        items = props
            .itemtions
            .map((item, idx) => {
                return <itemBloc item={item} key={item.name} {...props}/>;
                // the below fails to render any itemBlocs
                var ds = DragSource('item', spec, (connect, monitor) => {
                    return {
                        connectDragSource: connect.dragSource(),
                        item: item,
                        ...props
                    };
                });
                return ds(itemBloc);
            });
    }

    return <div className="itemsBar">{items}</div>;
}

CNSKnight avatar Mar 09 '17 16:03 CNSKnight

@CNSKnight This is quite interesting. I need to dig in more

// When we do this
const ItemBlockSource = DragSource('item', spec, collect)(ItemBloc);
// And use ItemBlockSource in place of ItemBloc it works

Complete example

const ItemBloc = props => {
  let item = props.item;
  return props.connectDragSource(
    <button>{item.name}</button>
  );
};

const spec = {
  beginDrag(props, monitor) {
    return {name: props.name};
  }
};

const collect = (connect, monitor) => {
  return {
    connectDragSource: connect.dragSource()
  };
};

const ItemBlockSource = DragSource('item', spec, collect)(ItemBloc)

const ItemsBar = props => {
  debugger;
  let items;
  if (!props.items) {
    items = <p>No Items to Display</p>;
  } else {
    items = props
      .items
      .map((item) => {
        return (<ItemBlockSource item={item} key={item.name} {...props}/>);
      });
  }

  return <div className="itemsBar">{items}</div>;
}

anilreddykatta avatar Mar 18 '17 23:03 anilreddykatta

@CNSKnight

let iBloc = <ItemBloc item={item} key={item.name} {...props}/>;
return DragSource('item', spec, collect)(iBloc);

It seems we are not initializing react element, second line is just returning DragSource class, neither createElement or JSX has been used to initialize wrapped component. As no call to createElement is made inside map, with DragSource(ItemBloc).

Let me know if this helps 😄

anilreddykatta avatar Mar 19 '17 02:03 anilreddykatta

I'm still having this issue

import * as React from "react";
import styled from "styled-components";
import {
  DragSource,
  DragSourceConnector,
  DragSourceMonitor,
  ConnectDragSource
} from "react-dnd";

const ImageHolder = styled.div`
  margin: 10px;
  max-width: 200px;
  width: 200px;
  min-height: 100px;
  position: relative;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
`;

const ImageNameHolder = styled.div`
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 10px;
  background-color: rgba(0, 0, 0, 0.5);
  font-size: 13px;
`;

interface Props {
  imagePath: string;
  connectDragSource?: ConnectDragSource;
  isDragging?: boolean;
}

@DragSource<Props>(
  "imageBeng",
  {
    beginDrag: (props: Props) => props.imagePath
  },
  (connect: DragSourceConnector, monitor: DragSourceMonitor) => ({
    connectDragSource: connect.dragSource(),
    isDragging: monitor.isDragging()
  })
)
export class PhotosBrowserPhoto extends React.Component<Props> {
  render() {
    const { imagePath, connectDragSource } = this.props;
    console.log({ connectDragSource });
    return connectDragSource(
      <div>drag me</div>
    );
  }
}

This components is not even rendered (ie. render function is not called)

pie6k avatar Sep 15 '18 14:09 pie6k

I solved this issue by adding a Provider as listed here:

https://github.com/react-dnd/react-dnd/issues/1132#issuecomment-426186825

theoutlander avatar Oct 02 '18 08:10 theoutlander

There should be some exception like You need to add provider - it'd save ton of time for me ;)

pie6k avatar Oct 02 '18 20:10 pie6k

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 06 '19 04:07 stale[bot]