Sorry if this is a dumb/obvious question, I'm new to Javascript/React. I'm trying to create a menu filled with JSON objects that I get from an API call. Since the JSON data is a bunch of nested objects (I'm talking hundreds of objects), I figured out how to recursively call the objects and make them show up in an unordered list. I can't seem to figure out how to add an onClick function that, when clicked, will show the next level down in the nested objects.
I will provide some screenshots to give you an idea of what I have and what I'm trying to achieve. The recursion part is tripping me up on how I can get this working. Does anyone have ideas?
class Menu extends React.Component {
state = {
categories: []
};
makeMenuLayer = layer => {
const layerKeys = Object.entries(layer).map(([key, value]) => (
<ul>
{key}
{this.makeMenuLayer(value)}
</ul>
));
return <div>{layerKeys}</div>;
};
componentDidMount() {
axios.get("https://www.ifixit.com/api/2.0/categories").then(response => {
this.setState({ categories: response.data });
});
}
render() {
const { categories } = this.state;
return <div>{this.makeMenuLayer(categories)}</div>;
}
}
this is what currently shows up with this code. I'm trying to make it so that only the first layer of objects show up, and then when each element is clicked on, the next level gets displayed. For example if I click on Apparel, It displays Accessory, Clothing, Eyeglasses, etc. and if I click on Accessory, it will display Necktie and Umbrella.
here's a screenshot of the data I'm working with displayed in the console

