【问题标题】:React getting access to properties of list component on click反应在单击时访问列表组件的属性
【发布时间】:2020-04-26 06:11:03
【问题描述】:

我正在用 React 创建我的第一个应用程序。这是一个电影院预订应用程序。我有一个组件,它是放映室中的单个座位,以及一个带有座位列表和预订的放映室组件。我需要访问用户将选择的座位 ID,并在单击提交按钮后将此数据发送到 api。我现在这样做的方式行不通。我认为这是因为我没有在screeningRoom 组件中呈现单个座位,而是在列表中呈现。

这是我的代码:

座椅组件

import React, { Component } from "react";
import "./seat.css";

class Seat extends Component {
  constructor(props) {
    super(props);
    this.state = {
      bgc: this.props.ticket ? "red" : "grey",
      seatId: ""
    };
    //console.log(this.props)
  }
  reserve = () => {
    if (!this.props.ticket) {
      this.state.bgc === "grey"
        ? this.setState({ bgc: "green" })
        : this.setState({ bgc: "grey" });
    }
    this.setState({ seatId: this.props.id });
    //console.log (this.props.id)
  };

  render() {
    return (
      <div>
        <input
          className="seat"
          style={{ backgroundColor: this.state.bgc }}
          onClick={this.reserve}
        ></input>
      </div>
    );
  }
}
export default Seat;

screeningRoom 组件

import React, { Component } from "react";
import axios from "axios";
import Seat from "./seats";
import "./screeningRoom.css";

export default class screeningRoom extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      seats: [],
      room: [],
      clicked: []
    };
    this.handleClick = this.handleClick.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  async componentDidMount() {
    this.setState({ loading: true });

    const {
      match: { params }
    } = this.props;

    const res = await axios.get(
      `http://localhost:8080/api/shows/${params.id}/seats`
    );
    //console.log(res)

    this.setState({ seats: res.data.seats });
    //console.log(this.state.seats)
    this.setState({ room: res.data.name });
  }
  renderSeats() {
    return this.state.seats.map(seat => (
      <Seat
        onClick={this.handleClick.bind(this)}
        key={seat._id}
        value={seat._id}
        ticket={seat.ticket}
        seat={seat}
      />
    ));
  }

  handleSubmit(e) {
    e.preventDefault();
    console.log(e.target.value);
  }
  handleClick(e) {
    console.log(e.currentTarget.value);
  }

  render() {
    return (
      <div
        className="screening-room d-flex flex-column justify-contnt-center align-items-center"
        style={{ marginTop: 20 }}
      >
        <h3 style={{ color: "white" }}>Room: {this.state.room} </h3>
        <div className="screen">screen</div>
        <form onSubmit={this.handleSubmit.bind(this)}>
          <div className={this.state.room}>{this.renderSeats()}</div>
          <input type="submit" value="Submit" />
        </form>
      </div>
    );
  }
}

【问题讨论】:

标签: javascript reactjs


【解决方案1】:

您没有将id 传递给Seat 组件。所以this.props.id 不存在是有道理的。

要么将 id 传递给组件:

<Seat 
  id={seat._id} 
  onClick={this.handleClick.bind(this)} 
  key={seat._id} 
  value={seat._id} 
  ticket={seat.ticket} 
  seat={seat}
/>

或者更改您要查找的道具,因为它们是相同的:

this.setState({seatId: this.props.value})

这是React docs on components and props 的链接。这是 React 非常基础和重要的部分,我建议从这里开始。

【讨论】:

  • 好的,但我将如何访问该 ID?控制台在点击时不会记录任何内容
  • 你指的是哪个日志?您有一个函数 handleClick 被绑定到 this 两次,但随后从未使用过。所以我不希望那个日志触发。在reserve 控制台日志被注释掉了?我假设你没有评论它?如果是这样,并且如果您按照我的建议进行更改以通过 id,它将在那里
  • 构造函数中的控制台登录也会失败。您需要记录props,而不是this.props。附带说明:您还应该将构造函数更改为此 bgc: props.ticket ? "red" : "grey"
  • 点击 Seat 组件可以正常工作。该组件中的所有内容都可以正常工作。我的问题是screeningRoom 组件。在座位组件中,用户单击座位项目,它的颜色变为绿色(保留)。然后我想在screeningRoom 组件中从API 中获取选择的座位ID 并使用提交按钮发布。
  • 非常感谢布赖恩! “prevState”正是我需要的,从现在开始就不知道了:)
猜你喜欢
  • 2021-01-22
  • 1970-01-01
  • 2017-01-22
  • 1970-01-01
  • 2020-09-17
  • 2017-06-28
  • 1970-01-01
  • 2011-04-06
  • 1970-01-01
相关资源
最近更新 更多