jakezatecky / react-checkbox-tree

A simple and elegant checkbox tree for React.
https://jakezatecky.github.io/react-checkbox-tree/
MIT License
709 stars 211 forks source link

Uncaught TypeError: Cannot read property 'map' of undefined #211

Closed shaileshy8787 closed 4 years ago

shaileshy8787 commented 4 years ago

Passign below json object in node getting issues.

[
    {
        "value": 1,
        "label": "Activity",
        "children": [
            {
                "value": 1,
                "label": "Walking & Running"
            },
            {
                "value": 2,
                "label": "Climbing"
            },
            {
                "value": 3,
                "label": "Steps"
            },
            {
                "value": 3,
                "label": "Workouts",
                "children": [
                    {
                        "value": 7,
                        "label": "Archery"
                    },
                    {
                        "value": 8,
                        "label": "Barre"
                    },
                    {
                        "value": 9,
                        "label": "Climbing"
                    },
                    {
                        "value": 10,
                        "label": "Core Training"
                    },
                    {
                        "value": 11,
                        "label": "Cross Country Skiing"
                    },
                    {
                        "value": 12,
                        "label": "Curling"
                    },
                    {
                        "value": 13,
                        "label": "Elliptical"
                    },
                    {
                        "value": 14,
                        "label": "Equestrian Sports"
                    },
                    {
                        "value": 15,
                        "label": "Fishing"
                    },
                    {
                        "value": 16,
                        "label": "Flexibility"
                    },
                    {
                        "value": 17,
                        "label": "Functional Strength Training"
                    },
                    {
                        "value": 18,
                        "label": "Gymnastics"
                    },
                    {
                        "value": 19,
                        "label": "Hand Cycling"
                    },
                    {
                        "value": 20,
                        "label": "High Intensity Interval Training"
                    },
                    {
                        "value": 21,
                        "label": "Hockey"
                    },
                    {
                        "value": 22,
                        "label": "Martial Arts"
                    },
                    {
                        "value": 23,
                        "label": "Mind And Body"
                    },
                    {
                        "value": 24,
                        "label": "Paddle Sports"
                    },
                    {
                        "value": 25,
                        "label": "Pilates"
                    },
                    {
                        "value": 26,
                        "label": "Preparation And Recovery"
                    },
                    {
                        "value": 27,
                        "label": "Rowing"
                    },
                    {
                        "value": 28,
                        "label": "Sailing"
                    },
                    {
                        "value": 29,
                        "label": "Skating Sports"
                    },
                    {
                        "value": 30,
                        "label": "Snow Sports"
                    },
                    {
                        "value": 31,
                        "label": "Surfing Sports"
                    },
                    {
                        "value": 32,
                        "label": "Tai Chi"
                    },
                    {
                        "value": 33,
                        "label": "Traditional Strength Training"
                    },
                    {
                        "value": 34,
                        "label": "Water Fitness"
                    },
                    {
                        "value": 35,
                        "label": "Water Polo"
                    },
                    {
                        "value": 36,
                        "label": "Water Sports"
                    },
                    {
                        "value": 37,
                        "label": "Wrestling"
                    },
                    {
                        "value": 38,
                        "label": "Yoga"
                    },
                    {
                        "value": 39,
                        "label": "Other"
                    },
                    {
                        "value": 1,
                        "label": "American Football",
                        "children": [
                            {
                                "value": 40,
                                "label": "American Football (km)"
                            },
                            {
                                "value": 79,
                                "label": "American Football (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 3,
                        "label": "Australian Football",
                        "children": [
                            {
                                "value": 41,
                                "label": "Australian Football (km)"
                            },
                            {
                                "value": 80,
                                "label": "Australian Football (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 4,
                        "label": "Badminton",
                        "children": [
                            {
                                "value": 42,
                                "label": "Badminton (km)"
                            },
                            {
                                "value": 81,
                                "label": "Badminton (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 5,
                        "label": "Baseball",
                        "children": [
                            {
                                "value": 43,
                                "label": "Baseball (km)"
                            },
                            {
                                "value": 82,
                                "label": "Baseball (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 6,
                        "label": "Basketball",
                        "children": [
                            {
                                "value": 44,
                                "label": "Basketball (km)"
                            },
                            {
                                "value": 83,
                                "label": "Basketball (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 7,
                        "label": "Bowling",
                        "children": [
                            {
                                "value": 45,
                                "label": "Bowling (km)"
                            },
                            {
                                "value": 84,
                                "label": "Bowling (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 8,
                        "label": "Boxing",
                        "children": [
                            {
                                "value": 46,
                                "label": "Boxing (km)"
                            },
                            {
                                "value": 85,
                                "label": "Boxing (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 10,
                        "label": "Cricket",
                        "children": [
                            {
                                "value": 47,
                                "label": "Cricket (km)"
                            },
                            {
                                "value": 86,
                                "label": "Cricket (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 11,
                        "label": "Cross Training",
                        "children": [
                            {
                                "value": 48,
                                "label": "Cross Training (km)"
                            },
                            {
                                "value": 87,
                                "label": "Cross Training (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 13,
                        "label": "Cycling",
                        "children": [
                            {
                                "value": 49,
                                "label": "Cycling (km)"
                            },
                            {
                                "value": 88,
                                "label": "Cycling (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 14,
                        "label": "Dance",
                        "children": [
                            {
                                "value": 50,
                                "label": "Dance (km)"
                            },
                            {
                                "value": 89,
                                "label": "Dance (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 61,
                        "label": "Downhill Skiing",
                        "children": [
                            {
                                "value": 51,
                                "label": "Downhill Skiing (km)"
                            },
                            {
                                "value": 90,
                                "label": "Downhill Skiing (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 18,
                        "label": "Fencing",
                        "children": [
                            {
                                "value": 52,
                                "label": "Fencing (km)"
                            },
                            {
                                "value": 91,
                                "label": "Fencing (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 21,
                        "label": "Golf",
                        "children": [
                            {
                                "value": 53,
                                "label": "Golf (km)"
                            },
                            {
                                "value": 92,
                                "label": "Golf (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 23,
                        "label": "Handball",
                        "children": [
                            {
                                "value": 54,
                                "label": "Handball (km)"
                            },
                            {
                                "value": 93,
                                "label": "Handball (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 24,
                        "label": "Hiking",
                        "children": [
                            {
                                "value": 55,
                                "label": "Hiking (km)"
                            },
                            {
                                "value": 94,
                                "label": "Hiking (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 26,
                        "label": "Hunting",
                        "children": [
                            {
                                "value": 56,
                                "label": "Hunting (km)"
                            },
                            {
                                "value": 95,
                                "label": "Hunting (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 64,
                        "label": "Jump Rope",
                        "children": [
                            {
                                "value": 57,
                                "label": "Jump Rope (km)"
                            },
                            {
                                "value": 96,
                                "label": "Jump Rope (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 65,
                        "label": "Kickboxing",
                        "children": [
                            {
                                "value": 58,
                                "label": "Kickboxing (km)"
                            },
                            {
                                "value": 97,
                                "label": "Kickboxing (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 27,
                        "label": "Lacrosse",
                        "children": [
                            {
                                "value": 59,
                                "label": "Lacrosse (km)"
                            },
                            {
                                "value": 98,
                                "label": "Lacrosse (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 73,
                        "label": "Mixed Cardio",
                        "children": [
                            {
                                "value": 60,
                                "label": "Mixed Cardio (km)"
                            },
                            {
                                "value": 99,
                                "label": "Mixed Cardio (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 32,
                        "label": "Play",
                        "children": [
                            {
                                "value": 61,
                                "label": "Play (km)"
                            },
                            {
                                "value": 100,
                                "label": "Play (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 34,
                        "label": "Racquetball",
                        "children": [
                            {
                                "value": 62,
                                "label": "Racquetball (km)"
                            },
                            {
                                "value": 101,
                                "label": "Racquetball (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 36,
                        "label": "Rugby",
                        "children": [
                            {
                                "value": 63,
                                "label": "Rugby (km)"
                            },
                            {
                                "value": 102,
                                "label": "Rugby (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 37,
                        "label": "Running",
                        "children": [
                            {
                                "value": 64,
                                "label": "Running (km)"
                            },
                            {
                                "value": 103,
                                "label": "Running (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 67,
                        "label": "Snowboarding",
                        "children": [
                            {
                                "value": 65,
                                "label": "Snowboarding (km)"
                            },
                            {
                                "value": 104,
                                "label": "Snowboarding (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 41,
                        "label": "Soccer",
                        "children": [
                            {
                                "value": 66,
                                "label": "Soccer (km)"
                            },
                            {
                                "value": 105,
                                "label": "Soccer (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 42,
                        "label": "Softball",
                        "children": [
                            {
                                "value": 67,
                                "label": "Softball (km)"
                            },
                            {
                                "value": 106,
                                "label": "Softball (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 43,
                        "label": "Squash",
                        "children": [
                            {
                                "value": 68,
                                "label": "Squash (km)"
                            },
                            {
                                "value": 107,
                                "label": "Squash (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 68,
                        "label": "Stairs",
                        "children": [
                            {
                                "value": 69,
                                "label": "Stairs (km)"
                            },
                            {
                                "value": 108,
                                "label": "Stairs (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 69,
                        "label": "Step Training",
                        "children": [
                            {
                                "value": 70,
                                "label": "Step Training (km)"
                            },
                            {
                                "value": 109,
                                "label": "Step Training (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 46,
                        "label": "Swimming",
                        "children": [
                            {
                                "value": 71,
                                "label": "Swimming (km)"
                            },
                            {
                                "value": 110,
                                "label": "Swimming (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 47,
                        "label": "Table Tennis",
                        "children": [
                            {
                                "value": 72,
                                "label": "Table Tennis (km)"
                            },
                            {
                                "value": 111,
                                "label": "Table Tennis (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 48,
                        "label": "Tennis",
                        "children": [
                            {
                                "value": 73,
                                "label": "Tennis (km)"
                            },
                            {
                                "value": 112,
                                "label": "Tennis (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 49,
                        "label": "Track And Field",
                        "children": [
                            {
                                "value": 74,
                                "label": "Track And Field (km)"
                            },
                            {
                                "value": 113,
                                "label": "Track And Field (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 51,
                        "label": "Volleyball",
                        "children": [
                            {
                                "value": 75,
                                "label": "Volleyball (km)"
                            },
                            {
                                "value": 114,
                                "label": "Volleyball (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 52,
                        "label": "Walking",
                        "children": [
                            {
                                "value": 76,
                                "label": "Walking (km)"
                            },
                            {
                                "value": 115,
                                "label": "Walking (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 71,
                        "label": "Wheelchair Run Pace",
                        "children": [
                            {
                                "value": 77,
                                "label": "Wheelchair Run Pace (km)"
                            },
                            {
                                "value": 116,
                                "label": "Wheelchair Run Pace (kcal)"
                            }
                        ]
                    },
                    {
                        "value": 70,
                        "label": "Wheelchair Walk Pace",
                        "children": [
                            {
                                "value": 78,
                                "label": "Wheelchair Walk Pace (km)"
                            },
                            {
                                "value": 117,
                                "label": "Wheelchair Walk Pace (kcal)"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "value": 2,
        "label": "Body Measurements",
        "children": [
            {
                "value": 4,
                "label": "Weight"
            }
        ]
    },
    {
        "value": 3,
        "label": "Sleep",
        "children": [
            {
                "value": 5,
                "label": "Sleep"
            }
        ]
    },
    {
        "value": 4,
        "label": "Vitals",
        "children": [
            {
                "value": 6,
                "label": "Heart Rate"
            }
        ]
    }
]
jakezatecky commented 4 years ago

This error happens because several of the nodes in your example have the same value (there are multiple nodes of value 1 for example). As stated in the README, the value for each node must be unique relative to all other nodes.

The next release of this component will throw a CheckboxTreeError when it detects duplicates to provide better troubleshooting.

ManjunathBE commented 4 years ago

any comment on my issue? I donot have any duplicates ..

/ eslint-disable no-unused-vars / import "jquery"; import React from "react"; import CheckboxTree from "react-checkbox-tree";

const data = [ { id: 1, name: "Europe", code: "EU", countries: [ { id: 679, name: "France", cities: [ { id: 2347, name: "Paris" }, { id: 2342, name: "Menton" }, ], }, { id: 432, name: "Ukraine", cities: [ { id: 32, name: "Kyiv" }, { id: 5678, name: "Lviv" }, ], }, ], }, { id: 2, name: "North America", code: "US", countries: [ { id: 67, name: "United States", cities: [ { id: 98, name: "Chicago" }, { id: 765, name: "New York" }, { id: 634, name: "Los Angeles" }, ], }, ], }, ];

const accessors = [ { label: "name", value: "id", leaves: "countries", type: "continent", }, { label: "name", value: "id", leaves: "cities", type: "country", }, { label: "name", value: "id", type: "city", }, ];

export default class TestTree extends React.Component { constructor(params) { super(params);

this.action = this.action.bind(this);

}

action(s) { console.log(this.refs.tree.getValues()); }

render() { return ( <div style={{ margin: "150px" }}> <CheckboxTree ref="tree" data={data} accessors={accessors} onChange={this.action} />

); } }

jakezatecky commented 4 years ago

@ManjunathBE can you post a live example showcasing this issue?

You can modify this CodeSandbox reference point to create a working example.

jakezatecky commented 4 years ago

Closing due to lack of activity. @ManjunathBE if you still have issues, please feel free to re-open with a live example that I can reference.