简体   繁体   中英

React change state based on select option

I need to store the distance value in state. It should equal the distance passed as props + the distance selected by user. How to do this?

class Distance extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      distance: 0
    };
  }

  onChange = e => {
  }

  render() {
    return (
      <div>
        <p>{this.props.distance}</p>
        <select onChange={this.onChange}>
          <option>30km</option>
          <option>50km</option>
          <option>70km</option>
        </select>
      </div>
    );
  }
}

Using functional components, you can do this:

  const Distance = () => {
  const [distance, setDistance] = useState("");

  return (
    <div>
      <p>{distance}</p>
      <select onChange={(e) => setDistance({distance: e.target.value})}>
        <option value="30">30km</option>
        <option value="50">50km</option>
        <option value="70">70km</option>
      </select>
    </div>
  );
};

export default Distance;

In a case where you have multiple inputs, and only distance is a select input, you can do this to update distance while maintaining the values of other inputs:

  const Distance = () => {
  const [input, setInput] = useState({
     distance: "",
     time: "",
     place: "",
  });

  return (
    <div>
      <p>{distance}</p>
      <select onChange={(e) => 
           setInput({ ...input, distance: e.target.value }}
       >
        <option value="30">30km</option>
        <option value="50">50km</option>
        <option value="70">70km</option>
      </select>
    </div>
  );
};

export default Distance;

First add value attributes to your <option> elements, and then access the use the value of the <select> via e.currentTarget.value in your onChange handler like so:

class Distance extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      distance: 0
    };
  }

  onChange = e => {

     // Extract value of select like so. Use parseInt for
     // improved type safety
     const valueSelectedByUser = parseInt(e.target.value);

     // Update distance in state via setState()
     this.setState({ distance : this.props.distance + valueSelectedByUser });
  }

  render() {
    return (
      <div>
        <p>{this.props.distance}</p>
        <select onChange={this.onChange}>
          <option value="30">30km</option>
          <option value="50">50km</option>
          <option value="70">70km</option>
        </select>
      </div>
    );
  }
}

You should include value for select and handle onChange event:

class Distance extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      distance: 0
    };
  }

  onChange = e => {
     this.setState({
       distance: this.props.distance ? this.props.distance + e.target.value : e.target.value
     });
  }

  render() {
    return (
      <div>
        <p>{this.props.distance}</p>
        <select onChange={this.onChange}>
          <option value="30">30km</option>
          <option value="50">50km</option>
          <option value="70">70km</option>
        </select>
      </div>
    );
  }
}

you can simply do this like this, I have converted your code to functional components and also modified it, try this.

    const Distance = () => {
  const [distance, setDistance] = useState("");

  return (
    <div>
      <p>{distance}</p>
      <select onChange={(e) => setDistance(e.target.value)}>
        <option value="30">30km</option>
        <option value="50">50km</option>
        <option value="70">70km</option>
      </select>
    </div>
  );
};

export default Distance;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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