Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142100

How to get multiple values from child component in Reactjs?

$
0
0

I want to get two values in single method of Parent Component. Pass the props value from Child to Parent Component. What is the appropriate solution?

  1. Form.js (Child Component)
 // First method -> Here I get the value in suggestion which is then
 // passed to Parent Component via props
  onSuggestionSelected = (event, {suggestion}) => {
    this.props.getWeather(suggestion)
  }

 // Second method -> Here is the value which I want to pass via 
 // props
  onClick = (e) => {
    e.preventDefault()
    const value = e.target.value
    this.props.getWeather(value)
  }

 // Autosuggest Input Component
 // Here I get suggestion value
 <Autosuggest
        onSuggestionSelected={this.onSuggestionSelected}
 /> 

 // Button Component
 // Here I pass the input value to onClick method
 <MDBBtn
        type="submit"
        value={inputProps.value} 
        onClick={e => this.onClick(e)}
  >
    Search Weather
  </MDBBtn>
  1. App.js (Parent Component)
getWeather = async (suggestion, value) => {
    // Here I get suggestion values
    const city = suggestion.name;
    const country = suggestion.country;

    // Here I get undefined
    console.log('VALUE', value)
}

So, as per code above, How to get both values from Child Component? Is it possible? Any suggestions or changes


Viewing all articles
Browse latest Browse all 142100

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>