S
S
Sergey Alekseev2018-02-15 13:04:05
React
Sergey Alekseev, 2018-02-15 13:04:05

React how to create a data structure?

Hello, I'm using react-sortable-tree. There is a list of objects and by clicking on this object, show its children:

import React, { Component } from 'react';
import SortableTree, { addNodeUnderParent, removeNodeAtPath } from 'react-sortable-tree';


const firstNames = [
  'Adidas',
  'Nike',
  'Rebook'
];


class App extends Component {
    constructor(props) {
        super(props);

        this.state = {
            treeData: [
                { title: 'Adidas', children: null},
                { title: 'Nike', children: null},
                { title: 'Rebook', children: null}
            ]
        };
    }

    render() {
        //const getNodeKey = ({ treeIndex }) => treeIndex;
        const getRandomName = () =>
          firstNames[Math.floor(Math.random() * firstNames.length)];
        return (
            <div>

                <div style={{ height: 300 }}>
                    <SortableTree
                        treeData={ this.state.treeData }
                        onChange={ () => {
                            this.state.treeData.map((node) => {
                                node.children;
                            })
                        }}

                    />
                </div>

                <button
                    onClick={() =>
                        this.setState(state => ({
                            treeData: state.treeData.concat({
                                title: `${getRandomName()} ${getRandomName()}sson`,
                            }),
                        }))
                    }
                    id="add"
                >
                  + ADD
                </button>
          </div>
        );
    }
}

How to solve such a problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question