简体   繁体   English

Reactjs:将文本注入“ React-Select输入”字段

[英]Reactjs: Inject a text into React-Select Input field

I am using react-select . 我正在使用react-select

Sometimes I need to update my react-select input field without interacting with the input directly (I need to inject text into it). 有时,我需要更新我的“反应选择”输入字段,而不直接与输入进行交互(我需要向其中注入文本)。 I can't seem to find a way to do this properly with the docs. 我似乎无法找到一种使用文档正确执行此操作的方法。

Here is my React-Select input: 这是我的React-Select输入:

<Select name='event-title-edit'  
  className="event-title-edit" 
  ref="stateSelect" autofocus 
  optionComponent={DropdownOptions} 
  optionRenderer={this.optionRenderer}  
  onInputChange={this.handleChangeTitle} 
  options={this.state.titleResults} simpleValue 
  clearable={this.state.clearable} 
  name="selected-state"  
  value={this.state.selectedTitleValue} 
  onChange={this.handleTitleChosenEdit} 
  onClose={this.onCloseTitle} 
  searchable={this.state.searchable} />

My options are dynamically created based on the results from elastic search. 我的选项是根据弹性搜索的结果动态创建的。 Also, my label and my value are two different strings (one is an "id" and one is a "name"). 另外,我的标签和我的值是两个不同的字符串(一个是“ id”,一个是“ name”)。

I need to be able to inject a name into react-select input box. 我需要能够将名称注入react-select输入框中。 How would I be able to do this? 我将如何做到这一点?

Get a ref to your select component 获取您所选组件的参考

<Select ref="select"...

Then call it's setValue method with the new value and it will trigger the onChange event 然后使用新值调用它的setValue方法,它将触发onChange事件

this.refs.select.setValue("hello")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM