Как мне нажать API для отображения ветви, когда я выбираю выпадающий список? - PullRequest
0 голосов
/ 13 февраля 2020
import React, { useState } from "react";
import axios from "axios";

const App = () => {
  const [value, setValue] = useState('');
  const [locations, setLocations] = useState([]);

  const handleSubmit = async () => {
    if (value === "") return;

    let response = await axios.get(`https://api2-4ofagodxfq-uc.a.run.app/locality?stateName=KARNATAKA&districtName=BANGALORE&pinCode=${value}`);
    if (response.status === 200) setLocations(response.data);
  }

  return <div>
    <select value="KARNATAKA">
      <option value="KARNATAKA">KARNATAKA</option>
    </select>
    <select value="BANGALORE">
      <option value="BANGALORE">BANGALORE</option>
    </select>
    PINCODE:<input type="text" onChange={e => setValue(e.target.value)} />
    <button type="button" onClick={handleSubmit}>SUBMIT</button>
    <select>
    {locations.map((location, index) => {
      return <option key={index}>{location}</option>
    })}
    </select>
  </div>
}

export default App;

API отображается здесь - https://api2-4ofagodxfq-uc.a.run.app/branch?stateName=KARNATAKA&districtName=BANGALORE&pinCode=560001&officeName=Rajbhavan%20S.O%20 (Бангалор)

...