反应中选择的问题

时间:2019-03-22 08:59:46

标签: reactjs

我想创建血液组选项的选择组件。我编写了代码,它确实在dom上呈现,但未显示在浏览器中。

这是FindDonor组件

import React, { Component } from "react";

class FindDonor extends Component {
  state = {
    value: "Find Donor By Blood Group"
  };

  handleChange = e => {
    this.setState({ value: e.target.value });
  };

  render() {
    return (
      <div className="findDonor">
        <h1>Hello</h1>
        <div className="input-field">
          <select value={this.state.value} onChange={this.handleChange}>
            <option value="a+">A+</option>
            <option value="o+">O+</option>
            <option value="b+">B+</option>
            <option value="ab+">AB+</option>
            <option value="a-">A-</option>
            <option value="o-">O-</option>
            <option value="b-">B-</option>
            <option value="ab-">AB-</option>
          </select>
        </div>
      </div>
    );
  }
}

export default FindDonor;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

家庭组件代码 Home.js

应用组件代码 App.js

2 个答案:

答案 0 :(得分:1)

您可能this.handleChange = this.handleChange.bind(this);中缺少constructor

从React文档(https://reactjs.org/docs/forms.html#the-select-tag)中查看以下示例:

class FlavorForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: 'coconut'};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    alert('Your favorite flavor is: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Pick your favorite flavor:
          <select value={this.state.value} onChange={this.handleChange}>
            <option value="grapefruit">Grapefruit</option>
            <option value="lime">Lime</option>
            <option value="coconut">Coconut</option>
            <option value="mango">Mango</option>
          </select>
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

答案 1 :(得分:0)

我认为您需要在onChange onChange={(e) => {this.handleChange(e)}上传递事件。另外,如果使用箭头功能,则无需绑定功能。

class FlavorForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: 'coconut'};
  }

  handleChange = (event) => {
    this.setState({value: event.target.value});
  }

  handleSubmit = (event) => {
    alert('Your favorite flavor is: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Pick your favorite flavor:
          <select value={this.state.value} onChange={(e) => {this.handleChange(e)}}>
            <option value="grapefruit">Grapefruit</option>
            <option value="lime">Lime</option>
            <option value="coconut">Coconut</option>
            <option value="mango">Mango</option>
          </select>
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}