0

I have a panel that should render list of options based on the type of query it gets through props. The query type is recognised OK but then it should map through the state and render a list of elements. But it renders nothing at all. Why is that? Thanks!

export default class SortSearchFilterPanel extends Component {
  constructor(props) {
    super(props);
    this.type = this.props.type;
    this.state = {
      sortList: [
        "What's New",
        "Name A-Z",
        "Name Z-A",
        "ABV Low To High",
        "ABV High To Low",
        "Price Low To High",
        "Price High To Low"
      ],
      filterList: ["Filter By Name", "Filter By Price"]
    };
  }

  render() {
    const { type } = this.props.type;
    const { sortList, filterList } = this.state;
    switch (type) {
      case "search":
        return (
          <div className="PanelGrid">
            <input type="text" name="serch" placeholder="Search..." />
          </div>
        );
        break;
      case "sort":
        return (
          <div className="PanelGrid">
            <ul className="PanelList">{sortList.map(item => <li key={item}>item</li>)}
            </ul>
          </div>
        );
        break;
      case "filter":
        return (
          <div className="PanelGrid">
            <ul className="PanelList">
              {filterList.map(item => <li key={item}>item</li>)}
            </ul>
          </div>
        );
        break;
      default:
        return null;
        break;
    }
  }
}
2
  • try to debug in switch case is type value is coming or not ? Commented Apr 8, 2020 at 12:03
  • Are you getting at least the <div className="PanelGrid">? Commented Apr 8, 2020 at 12:03

1 Answer 1

1

I believe your problem is in the declaration of type prop:

You'got this:

const { type } = this.props.type;

It should be this:

const { type } = this.props;
Sign up to request clarification or add additional context in comments.

1 Comment

well spotted! typical rookie mistake I guess... Thanks for the quick fix.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.