【问题标题】:How to change selected value of React-Bootstrap-Typeahead from HOC如何从 HOC 更改 React-Bootstrap-Typeahead 的选定值
【发布时间】:2019-06-27 03:42:45
【问题描述】:

我有两个 Typeahead (react-bootstrap-typeahead) 组件,我想在按下按钮时反转选定的值。以状态存储的数据正确变化。但我无法将更改的选择设置为 Typeahead 的值。

使用selected prop 设置选定对象但出现一些错误:

Warning: [react-bootstrap-typeahead] `defaultInputValue` will be overridden by the value from `selected`.

Warning: Failed prop type: Invalid prop `selected` supplied to `TypeaheadContainer(WrappedTypeahead)`.

我的 HOC

<TypeaheadComponent
    onSearch={value => getOriginPlaces(value, index)}
    data={route.originResults}
    onInputChange={value => originPlaceInputOnChange(value, index)}
    onSelect={item => originPlaceOnSelect(item, index)}
    selected={route.originPlace} // object
    inputValue={route.originPlaceInputValue} // string from  originPlaceInputOnChange()
/>

和 AsyncTypeahead (react-bootstrap-typeahead)

<AsyncTypeahead
    promptText={<PromptText />}
    searchText={<Loading />}
    emptyLabel={<NotFound />}
    inputProps={{...}}
    options={props.data}
    maxResults={10}
    paginationText={...}
    labelKey="name"
    filterBy={[...]}
    onSearch={props.onSearch}
    minLength={2}
    selectHintOnEnter={true}
    onInputChange={props.onInputChange}
    onChange={selected => props.onSelect(selected[0])} // set first object of array
    defaultInputValue={props.inputValue || ''}
    selected={props.selected} // object
    renderMenu={...}
/>

我该怎么做?或者我哪里错了

【问题讨论】:

    标签: reactjs typeahead react-bootstrap-typeahead


    【解决方案1】:

    您的主要问题是 selected 需要是一个数组,而不是一个对象。尝试将包含选择的数组(例如:[props.selected])传递给 AsyncTypeahead。应该可以的。

    对于第二个警告,defaultInputValue 将仅在 typeahead 最初装载(而不是后续渲染)时设置输入值,并且从selected 派生的任何输入值都将优先。

    【讨论】:

    • 嗨@ericgio,实际上你想在'selected'道具中实现什么?因为我有点从你的'React-Async-TypeAhead'中获得灵感来实现我自己的东西。就我而言,如果更改选择并设置了选择值,我需要启动一些 api。谢谢!
    最近更新 更多