【发布时间】:2020-03-18 14:53:42
【问题描述】:
我想在父组件的单一方法中获取两个值。将 props 值从子组件传递给父组件。什么是合适的解决方案?
- Form.js(子组件)
// 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>
- App.js(父组件)
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)
}
那么,根据上面的代码,如何从子组件中获取两个值?可能吗?任何建议或更改
【问题讨论】:
标签: javascript reactjs