反应条件渲染-为什么此代码不渲染任何东西?

时间:2020-04-08 11:59:33

标签: reactjs switch-statement conditional-statements rendering

我有一个面板,该面板应根据通过道具获得的查询类型呈现选项列表。可以将查询类型识别为OK,但是它应该映射状态并呈现元素列表。但是它什么也没有。这是为什么?谢谢!

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;
    }
  }
}

1 个答案:

答案 0 :(得分:1)

我相信您的问题出在prop类型的声明中:

您得到了:

const { type } = this.props.type;

应该是这样:

const { type } = this.props;