简体   繁体   English

如何在点击时使用传入的道具渲染 React 组件

[英]How to render React component with passed in props on click

I am making a Menu using React and Redux but am currently having an issue rendering the items when the user selects a category.我正在使用 React 和 Redux 制作菜单,但当前在用户选择类别时呈现项目时遇到问题。

My goal is to render a list of items nested in the redux state of a particular category when it is clicked.我的目标是在单击时呈现嵌套在特定类别的 redux 状态中的项目列表。

In Categories.js, it loads the categories from the redux store and displays them.在 Categories.js 中,它从 redux 存储中加载类别并显示它们。

import React, { Component } from "react";
import { connect } from "react-redux";
import CategoryItems from "./CategoryItems"
import {
  Card,
  CardTitle,
  CardImg,
  Container,
  Row,
  Col,
  CardBody,
  Button
} from "shards-react";

class Categories extends Component {
  handleClick = category => {

    alert(category.title);

   return (
     <CategoryItems 
      category={category}
     />

   );

  };

  render() {
    let categoryList = this.props.categories.map(category => {
      return (
        <div key={category.id}>
          <Col>
            <Card
              key={category.id}
              onClick={() => {
                this.handleClick(category);
              }}
            >
              <CardBody>
                <CardTitle>{category.title}</CardTitle>
              </CardBody>
            </Card>
          </Col>
        </div>
      );
    });
    return (
      <Container>
        <Row>{categoryList}</Row>
      </Container>
    );
  }
}

const mapStateToProps = state => {
  return {
    categories: state.categories,


  };
};

export default connect(mapStateToProps)(Categories);

When a category is clicked, (the alert was just so I could make sure data made it) I have it set to render the items Array nested within the selected category.当一个类别被点击时,(警报只是为了确保数据成功)我将它设置为呈现嵌套在所选类别中的项目数组。

import React, { Component } from 'react'
import {
    Card,
    CardTitle,
    CardImg,
    Container,
    Row,
    Col,
    CardBody,
    Button
  } from "shards-react";



export class CategoryItems extends Component {
    render() {
        let items = this.props.category.items;

        let categoryItems = items.map(item => {
            return (
              <Col className="">
                <Card
                  className="mt-2 mb-2 item-col"
                  style={{ maxWidth: "500px" }}
                  key={item.id}
                >
                  <CardBody>
                    <CardTitle style={{ position: "absolute", top: 20, right: 20 }}>
                      {item.title}
                    </CardTitle>
                    <CardImg
                      style={{ maxWidth: "200px" }}
                      src={item.img}
                      alt={item.title}
                    />
                    <span
                      style={{ position: "absolute", bottom: 40, right: 100 }}
                      to="/"
                      // onClick={() => {
                      //   this.handleClick(item.id);
                      // }}
                    >
                      <Button pill theme="info">
                        +
                      </Button>
                    </span>
                    <div style={{ position: "absolute", bottom: 40, right: 20 }}>
                      ${item.price}
                    </div>
                  </CardBody>
                </Card>
              </Col>
            );
          });
          return (
            <Container className="menu-item-cont">
              <Row>{categoryItems}</Row>
            </Container>
          );
    }
}

export default CategoryItems

This part does not render the items and I don't get an error message.这部分不呈现项目,我没有收到错误消息。

I have also tried placing the items I would like rendered directly into the state just to see if I could get them to render on click similar to how the Categories.js is with no luck.我还尝试将我想要渲染的项目直接放入 state 中,看看是否可以让它们在单击时渲染,就像 Categories.js 没有运气一样。

I am somewhat new to react so please forgive me if this is a beginner question.我对反应有点陌生,所以如果这是一个初学者问题,请原谅我。

Any help is appreciated as I have spent hours trying to figure this out.感谢任何帮助,因为我花了几个小时试图解决这个问题。

The problem is that you're not rendering what gets returned from handleClick .问题是您没有渲染从handleClick返回的内容。 What you need is a conditional render in the JSX, then determine whether or not to render the elements depending on a controlled flag variable in the state.您需要的是 JSX 中的条件渲染,然后根据状态中的受控标志变量确定是否渲染元素。

class Categories extends Component {
  constructor() {
    this.state = {
      show: false,
      category: undefined
    }

    this.toggle = (category) => {
      this.setState({ show: !this.state.show, category })
    }
  }

  render() {
    let categoryList = this.props.categories.map(category => {
      return (
        <div key={category.id}>
          <Col>
            <Card
              key={category.id}
              onClick={e => this.toggle(category)}
            >
              <CardBody>
                <CardTitle>{category.title}</CardTitle>
              </CardBody>
            </Card>
          </Col>
        </div>
      );
    });
    return ([
      <Container>
        <Row>{categoryList}</Row>
      </Container>,
      (
        this.state.show 
        ? <CategoryItems category={this.state.category}/> 
        : null
      )
    ]);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM