如何访问道具和反应状态 - 选择HOC

时间:2018-04-30 15:35:12

标签: javascript reactjs react-select higher-order-components

我正在使用react-select v2.0创建带有预定义项目的选择下拉列表。我将它连接到Parse查询,该查询通过文本搜索返回选项。

我的问题是我无法弄清楚如何将所选值传递给父组件。

该组件名为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}
      />
    )
  }
}

调用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

如果我检查组件,我可以看到输入value属性与键入的文本匹配,但是当从下拉列表中选择一个值时,它会消失(即从<input value="Typed name" />转到<input value />一个单独的<span>元素与标签的值一起出现,但我不想从DOM中获取它,这似乎不是预期的方法。

如果我在React控制台选项卡中搜索我的组件,RestaurantSelect找不到任何内容:

Blank results for component search

但是,如果我搜索选择,它会显示并且propsstate具有所选值(在这种情况下为“Time 4 Thai”):

Component result with props and state

但是,RestaurantSelect中的console.log(this.state)仅显示inputValue,而<Select/>只显示

有没有办法访问高阶组件的propsstate

1 个答案:

答案 0 :(得分:0)

发现问题,在RestaurantSelect handleInputChange函数需要添加为返回组件的onChange道具。像这样:

  <AsyncSelect
    className="select-add-user-restaurant"
    cacheOptions
    defaultOptions
    placeholder="Start typing to select restaurant"
    loadOptions={promiseOptions}
    onChange={this.handleInputChange}
  />

newValue是具有这种结构的对象:

{
  value: "name",
  label: "Name"
}

注意:一旦激活,上面的代码就会抛出错误。我将其更改为将数据传递给父组件:

handleInputChange = (newValue: string) => {
  this.props.setRestaurantSelection(newValue)
  const inputValue = newValue
  this.setState({ inputValue })
  return inputValue
}

this.props.setRestaurantSelection来自父组件,如下所示:

<RestaurantSelect setRestaurantSelection={this.setRestaurantSelection} />

在父组件中看起来像这样:

constructor() {
  super()

  this.state = {
    restaurantSlug: ''
  }

  this.setRestaurantSelection = this.setRestaurantSelection.bind(this)
}

…

setRestaurantSelection = (value) => {
  this.setState({
    restaurantSlug: value.value
  })
}
相关问题