I am trying to implement drag and drop in react and if I call setState in the onDragOver handler (to be able to do some styling based on the drag state), onDrop will not be called on the second drop. If I remove the call to setState it works fine. What puzzles me is that it works the first time. I would expect that if it doesn't work it just wouldn't work at all.
Note: I am using react this with styled components.
I have a class based component as follows:
state = {
status: STATUS.NOT_DRAGGING,
};
onDragOver = (e) => {
e.preventDefault();
// this line is causing all the trouble
// this.setState(() => ({ status: STATUS.DRAGGING }));
};
onDrop = (e, rowId) => {
// get the item being dragged using dto
const id = e.dataTransfer.getData('id');
const name = e.dataTransfer.getData('name');
// clear out the data transfer array
e.dataTransfer.clearData();
if (id === null || id === 'undefined') return;
const node = { id, name };
const settings = this.props.getPlotContainer(rowId).settings;
// add node to plot
this.props.addNodeToPlot(rowId, node, settings);
this.setState({ status: STATUS.DROPPED });
};
and my droppable div:
<MainContainer
status={this.state.status}
className="droppable"
onDragOver={e => this.onDragOver(e)}
onDrop={e => this.onDrop(e, rowId)}
>
{(plot && plot.nodes.length === 0) && <EmptyPlot status={this.state.status}>{message}</EmptyPlot>}
{(plot && plot.nodes.length > 0) && this.renderPlotSettings(plot, rowId)}
{(plot && plot.nodes.length > 0) && this.renderPlot(plot)}
{(plot && plot.nodes.length > 0) && this.renderSeriesSettings(plot, rowId)}
</MainContainer>
Any advice or tips greatly appreciated!