【问题标题】:Type Element[] is not assignable to type never[] in react-bootstrap-typeahead类型 Element[] 不可分配给 react-bootstrap-typeahead 中的类型 never[]
【发布时间】:2021-02-01 06:28:02
【问题描述】:

我正在尝试在反应中创建一个预先输入的下拉菜单。下拉列表将显示名称列表。 我正在关注 npm 文档中的单一选择示例 live example

这是我的代码块。

const [singleSelections, setSingleSelections] = useState([]);

<Form.Group>
    <Form.Label>Selection</Form.Label>
        <Typeahead
            id="basic-typeahead-single"
            labelKey={option => `${option.key}`}
            onChange={selected => setSingleSelections(selected)}                                  
            options={personList.map(p => <option key={p.firstName} value={p.firstName!!}>{p.firstName}</option>)}
                                placeholder="Search Person"
            selected={singleSelections}
                            />                          
</Form.Group>                            

但是在这里我从 onChange 那里得到一个错误提示

“Element[]”类型的参数不能分配给“SetStateAction”类型的参数

我该如何解决这个问题。有人可以帮我吗

【问题讨论】:

    标签: reactjs bootstrap-typeahead


    【解决方案1】:

    当你写useState([])时,你必须通过useState&lt;T&gt;([])告诉typescript“singleSelections”的类型。由于您刚刚将 [] 作为值传递,因此打字稿将其评估为 never[]

    const [singleSelections, setSingleSelections] = useState&lt;TypeOfsingleSelection[]&gt;([])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-29
      • 2021-07-02
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 2022-08-14
      • 1970-01-01
      • 2020-03-08
      相关资源
      最近更新 更多