简体   繁体   中英

How to check if <InputB /> has a value after a value for <InputA /> has been selected?

I have the two following input components:

function Inputs() {
  return (      
    <div>
      <GooglePlacesAutocomplete
        onSelect={(data) => getPointA(data)}
        placeholder="Enter place or address for Point A"
      />

      <GooglePlacesAutocomplete
        onSelect={(data) => getPointB(data)}
        placeholder="Enter place or address for Point B"
        idPrefix="pointB"
      />
    </div>
  )
}

When I select a value from the first GooglePlacesAutocomplete component, I want to check if the second GooglePlacesAutocomplete component has a value. How can I do this? Here is a link to the component I'm using: https://www.npmjs.com/package/react-google-places-autocomplete

Thanks!

You can store the state of your selected values in you Inputs component so you have access to the selected values:

function Inputs() {
  const [placeA, setPlaceA] = useState('');
  const [placeB, setPlaceB] = useState('');
  const getPointA = (data) => {
    setPlaceA(data)
  }
  const getPointB = (data) => {
    setPlaceB(data)
  }
  return (      
    <div>
      <GooglePlacesAutocomplete
        onSelect={(data) => getPointA(data)}
        placeholder="Enter place or address for Point A"
      />

      <GooglePlacesAutocomplete
        onSelect={(data) => getPointB(data)}
        placeholder="Enter place or address for Point B"
        idPrefix="pointB"
      />
    </div>
  )
}

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