import React, { PureComponent } from "react"; class Dropdown extends PureComponent { constructor(props) { // spelling mistake change for constructor super(props); this.state = { isOpen: this.props.isInitialStateOpen, }; } /** * Made this an arrow function for binding of this function with the class, * otherwise it will throw error saying this is undefined. */ toggle = () => { const { isOpen } = this.state; /** * Here the state was not toggled, it was being set same as prev state. * Changed it to toggle the state */ this.setState({ isOpen: !isOpen }); }; render() { const { isOpen } = this.state; const { label } = this.props; /** * Improvement * Moved the open class on the list to the parent container, * so that the label can be shown as active dropdown */ return (
); } } class DropdownItem extends PureComponent { render() { /** * Added return statement here, which is required. */ return {this.props.children}; } } class ExampleNav extends PureComponent { render() { return ( ); } } /** * Improvements that can be done. * * 1) Add default state ability to dropdown. * Suppose you want to display first dropdown open by default, * then we can pass a prop to the component for that */ export default ExampleNav;