[英]How to access props and state of react-select HOC
I'm using react-select v2.0 to create a select dropdown with pre-defined items. 我正在使用react-select v2.0创建带有预定义项目的选择下拉列表。 I have it connected to a Parse query that returns the options with a text search.
我将其连接到一个Parse查询,该查询返回带有文本搜索的选项。
My issue is I cannot figure out how to pass the selected value up to the parent component. 我的问题是我无法弄清楚如何将所选值传递给父组件。
The component is named RestaurantSelect
and looks like this (abridged): 该组件名为
RestaurantSelect
,看起来像这样(经过删节):
import React, { Component } from 'react'
import AsyncSelect from 'react-select/lib/Async'
type State = {
inputValue: string
}
const filterRestaurants = (inputValue: string) => {
return (
// ... results from Parse query (this works fine)
)
}
const promiseOptions = inputValue => (
new Promise(resolve => {
resolve(filterRestaurants(inputValue))
})
)
export default class WithPromises extends Component<*, State> {
state = { inputValue: '' }
handleInputChange = (newValue: string) => {
const inputValue = newValue.replace(/\W/g, '')
this.setState({ inputValue })
return inputValue
}
render() {
return (
<AsyncSelect
className="select-add-user-restaurant"
cacheOptions
defaultOptions
placeholder="Start typing to select restaurant"
loadOptions={promiseOptions}
/>
)
}
}
The parent component that calls RestaurantSelect
looks like this: 调用
RestaurantSelect
的父组件如下所示:
import React from 'react'
import RestaurantSelect from './RestaurantSelect'
class AddUserRestaurant extends React.Component {
constructor() {
super()
this.state = {
name: ''
}
}
addUserRestaurant(event) {
event.preventDefault()
// NEED INPUT VALUE HERE!
}
render() {
return (
<form onSubmit={(e) => this.addUserRestaurant(e)}>
<RestaurantSelect />
<button type="submit">Add</button>
</form>
)
}
}
export default AddUserRestaurant
If I inspect the component I can see the input value
attribute match the typed text, but when a value is selected from the dropdown it disappears (ie goes from <input value="Typed name" />
to <input value />
. A separate <span>
element appears with the value of the label, but I don't want to have to grab that from the DOM, that doesn't seem to be the intended method. 如果检查组件,则可以看到输入
value
属性与键入的文本匹配,但是当从下拉列表中选择一个值时,该值就会消失(即从<input value="Typed name" />
变为<input value />
。)单独的<span>
元素与标签的值一起出现,但是我不想从DOM中获取它,这似乎不是预期的方法。
If I search the React console tab for my component, RestaurantSelect
nothing is found: 如果我在React控制台选项卡中搜索我的组件,则
RestaurantSelect
找不到任何内容:
But, if I search for Select, it appears and has props
and state
that have the selected value ("Time 4 Thai" in this case): 但是,如果我搜索“选择”,它将显示并具有具有选定值的
props
和state
(在这种情况下为“ Time 4 Thai”):
However, console.log(this.state)
in RestaurantSelect shows only the inputValue
, nothing from <Select/>
但是,RestaurantSelect中的
console.log(this.state)
仅显示inputValue
, <Select/>
没有任何内容
Is there a way to access the props
and state
of the higher order component? 有没有办法访问高阶组件的
props
和state
?
Found the problem, in RestaurantSelect
the handleInputChange
function needs to be added as the onChange
prop to the returned component . 发现了问题,在
RestaurantSelect
,需要将handleInputChange
函数作为onChange
handleInputChange
添加到返回的组件 。 Like so: 像这样:
<AsyncSelect
className="select-add-user-restaurant"
cacheOptions
defaultOptions
placeholder="Start typing to select restaurant"
loadOptions={promiseOptions}
onChange={this.handleInputChange}
/>
newValue
is an object with this construction: newValue
是具有以下构造的对象:
{
value: "name",
label: "Name"
}
Note: Once activated, the code above throws an error. 注意:一旦激活,上面的代码将引发错误。 I changed it to this to pass the data up to the parent component:
我将其更改为此,以将数据传递到父组件:
handleInputChange = (newValue: string) => {
this.props.setRestaurantSelection(newValue)
const inputValue = newValue
this.setState({ inputValue })
return inputValue
}
Where this.props.setRestaurantSelection
comes from the parent component like so: 这里
this.props.setRestaurantSelection
来自父组件,如下所示:
<RestaurantSelect setRestaurantSelection={this.setRestaurantSelection} />
And looks like this in the parent component: 并在父组件中看起来像这样:
constructor() {
super()
this.state = {
restaurantSlug: ''
}
this.setRestaurantSelection = this.setRestaurantSelection.bind(this)
}
…
setRestaurantSelection = (value) => {
this.setState({
restaurantSlug: value.value
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.