I'm trying to make an accordion app with react, i have data coming in from an API and i have the basic outline of the app but i'm not sure how to handle the click on the accordion.
This is my code:
This is where i set the data
App.js
class App extends Component {
constructor(){
super();
this.state = {
myData: {}
};
}
componentDidMount() {
axios.get(linkToApi)
.then(responseData => {
this.setState({ mydata: responseData.data });
})
.catch(error => {
console.log("Porblem getting data", error);
});
}
render() {
return (
<div className="App">
<Accordion data={this.state.myData} />
</div>
);
}
}
export default App;
Accordion.js
const Accordion = props => {
let accordionElements = Object.keys(props.data).map(function(keyName, keyIndex) {
return <AccordionElement
{...props.data[keyName]}
key={props.data[keyName].id}
/>;
})
return (
<ul className="accordion">
{accordionElements}
</ul>
);
}
export default Accordion;
AccordionElement.js
const AccordionElement = props => {
const handleOnClick = (e) => {
e.preventDefault();
//
}
return (
<li style={listItemStyle} onClick={handleOnClick}>
<h1 data={props}>{ props.name }</h1>
<ul style={descriptionStyle}>
<li>Description: { props.description }</li>
</ul>
</li>
);
}
export default AccordionElement;
I want to be able to show or hide the description under it when the heading is clicked. I'm not really sure how i would go about this, any ideas?