JavaScript React-dnd - Not sure how to get drag and drop to fire for nested components of the same type

I'm trying to build a react nested list component that is fully draggable with react-dnd. I have a menu component which contains item components, and each item can also contain other item components. Unfortunately I don't know how to make a node with children draggable or any of its children nodes. Right now I have gotten nodes without childrendraggable, which is a good start, but then fails with:

TypeError: node is undefined

any time I try to drag any children. I used the react-dnd simple sortable example as a reference, but that doesn't contain any nesting.

Here is what I have so far:

Menu.js

//React DnD
var DragDropContext = require('react-dnd').DragDropContext;
var HTML5Backend = require('react-dnd-html5-backend');

//Item
var Item = require('./item');

var Menu = React.createClass({
    getInitialState() {
        return {
            currentNode: this.props.data,
            items: [],
        };
    },
    _clicked(child) {
        this.setState({
            currentNode: child,
        });
    },
    componentDidMount() {
        this._updateData();
    },
   _updateData: function(list) {
        var $this = this;

        if(_.isUndefined(list)){
            var children = $this.props.data.children;
        }
        else{
            if(_.isEmpty(list.children)){
                var children = null;
            }
            else{
                var children = list.children;
            }
        }

        if(children != null){
            var items = children.map(function(item, i) {
                return (<Item
                    key={item.id}
                    id={item.id}
                    child={item}
                    showChildren={this.props.showFirstChildren}
                    clickable={true}
                    onClick={$this._updateData}
                    swapItems={this.swapItems}
                />);
            }.bind(this));

            
            if(!_.isUndefined(list)){
                $this.setState({
                    currentNode: list,
                });
            }
            
            $this.setState({ 
                items: items,
            });
            
        }
    },
    compareItems: function(item1, item2){
        return item1.position - item2.position;
    },
    swapItems: function(id1, id2) {
        var $this = this;

        var items = this.state.currentNode.children;
        var item1 = items.filter(function(i){return i.id === id1})[0];
        var item2 = items.filter(function(i){return i.id === id2})[0];

        var item1Pos = item1.position;
        item1.position = item2.position;
        item2.position = item1Pos;

        items.sort(this.compareItems);

        var newItems = items.map(function(item, i) {
            return (<Item
                key={item.id}
                id={item.id}
                child={item}
                showChildren={this.props.showFirstChildren}
                clickable={true}
                onClick={$this._updateData}
                swapItems={this.swapItems}
            />);
        }.bind(this));

        this.setState({
            items: newItems,
        });
    },
    render() {
        return (
            <div>
                {this.state.currentNode.name}
                <ul>
                    {this.state.items}
                </ul>
            </div>
        );
    },
});

module.exports = DragDropContext(HTML5Backend)(Menu);
Answer:1

I've got the same problem when I try to manipulate tree before drag end. I.e. change leafs order before dragEnd() throw me the same error. But plain list works perfectly. I stop to change tree during DnD (our designer said - it is not good UX: jumping to mouse pointer subtree). So my solution: during DnD just change classes for target and source sub-trees (but do not change leafs order), like:

  .source-dnd {
    opacity: 0.2;
  }

  .target-dnd-add {
    border: 1px dotted #aaa;
  }

  .target-dnd-after {
    border-bottom: 1px dotted #aaa;
  }

  .target-dnd-before {
    border-top: 1px dotted #aaa;
  }

Also don't forget about monitor.isOver({ shallow: true })

Answer:2

I am trying to initialize an array of boolean values, where there is a different value on a specific position in array. If I initialize state like this, empty is array. state = { activeItems: [.....

I am trying to initialize an array of boolean values, where there is a different value on a specific position in array. If I initialize state like this, empty is array. state = { activeItems: [.....

  1. react usestate initialize with function
  2. react usestate initialize
  3. reactjs state initialize
  4. reactjs initialize state from props
  5. react js initialize state
  6. react initialise state
  7. react js initialise state

What I want to do is really simple and there is a syntax error that I can't understand: I have an array that contains some objects, and I want to create another array using some value of those ...

What I want to do is really simple and there is a syntax error that I can't understand: I have an array that contains some objects, and I want to create another array using some value of those ...

  1. create object from array javascript
  2. create object from class
  3. create object from interface typescript
  4. create object from json
  5. create object from string
  6. create object from type c#
  7. create object from class python
  8. create object from dict python
  9. create object from spreadsheet salesforce
  10. create object from object javascript
  11. create object from string python
  12. create object from map javascript
  13. create object from class javascript
  14. create object from array lodash
  15. create object from json string
  16. create object from xml c#
  17. create object from another object javascript
  18. create object from interface java
  19. create object from file java
  20. create object from json java

I have very little javascript skills and I would like to pass lines of a file as String arguments to a pre-written function. Basically what I want to do is read a file in this type of format, with ...

I have very little javascript skills and I would like to pass lines of a file as String arguments to a pre-written function. Basically what I want to do is read a file in this type of format, with ...

I'm in JavaScript for a while, but there is still soo much things to learn. Now, I had found some interesting facts about Switch statement. I know that when you use case someValue: without break; ...

I'm in JavaScript for a while, but there is still soo much things to learn. Now, I had found some interesting facts about Switch statement. I know that when you use case someValue: without break; ...