I'm not sure what I could do to make this work...So I'm calling API data (which is just a bunch of nested json objects) to create a menu. So far I have it working so that it renders the first layer of objects (first img), and when you click on each element, the next level shows up below it (second img). This works recursively, so I assume it to work for the next level down when I click on an element in "level 2", but this is not the case.
Can anyone see what I could change in my code to make this work?
class Menu extends React.Component {
state = {
categories: [],
list: ""
};
//handle displaying list of values when clicking on button
//search for list of values within object
handleSearch = (obj, next, event) => {
// console.log(event.target.name);
Object.keys(obj).forEach(key => {
if (typeof obj[key] === "object") {
if (next === key) {
//create DOM CHILDREN
createData(Object.keys(obj[key]), key, this.test, event);
}
this.handleSearch(obj[key], next);
}
});
};
componentDidMount() {
axios.get("https://www.ifixit.com/api/2.0/categories").then(response => {
this.setState({ categories: response.data });
});
}
render() {
return (
<React.Fragment>
{/* display list of things */}
<div className="columns is-multiline">
{Object.keys(this.state.categories).map(key => (
<div className="column is-4">
<div
onClick={event =>
this.handleSearch(this.state.categories, key, event)
}
>
{key}
</div>
<div name={key + "1"} />
</div>
))}
</div>
</React.Fragment>
);
}
}
and here is the code for createData, which creates the next level of data and is supposed to make it so these elements can be clicked to show the next level
var index = 1;
export function createData(data, key, search, e) {
e.stopPropagation();
let parent = document.getElementsByName(key + "1");
//create elements and append them
for (let i = 0; i < data.length; i++) {
let wrapper = document.createElement("ul");
wrapper.innerHTML = data[i];
wrapper.name = data[i] + index + 1;
wrapper.addEventListener("click", search(data[i]));
parent[0].append(wrapper);
}
}
here's a screenshot of "categories" in the console (objects within objects within objects)


