简体   繁体   English

在react-select中选择选项时,输入值不会改变

[英]Input Value doesn't change when selecting an option in react-select

I'm using react-select to define a select input control in my react application. 我正在使用react-select在我的react应用程序中定义一个select输入控件。 This is how I'm using the component: 这就是我使用该组件的方式:

<Select
   onChange={function(e) {console.log(e)}}
   options={[
      {value: "sf", label: "San Francisco"},
      {value: "nyc", label: "New York City"}
   ]}
></Select>

Before selecting any option I see a Select... placeholder as the input value. 在选择任何选项之前,我看到Select...占位符作为输入值。 This doesn't change after selecting an option: the input value doesn't change and the Select... placeholder appears to be the selected "option". 选择一个选项后,这不会改变:输入值不会改变, Select...占位符似乎是所选的“选项”。

Is there something wrong with how I'm using the component? 我如何使用该组件有什么问题吗?

Define the Select value in state variable, and logChange function will return an object , assign the value of that object to state variable, it will work, Check this code: 定义Select中值state变量, logChange函数会返回一个object ,指定value是的object ,以状态变量,它会工作,检查该代码:

 class App extends React.Component{ constructor(){ super(); this.state = {value: ''} } logChange(val) { console.log("Selected: " + val.value); this.setState({value: val.value}); } render(){ var options = [ {value: 'one', label: 'One' }, {value: 'two', label: 'Two' } ]; return( <Select name="form-field-name" value={this.state.value} options={options} onChange={this.logChange.bind(this)} /> ) } } ReactDOM.render(<App/>, document.getElementById('app')) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <script src="https://unpkg.com/classnames/index.js"></script> <script src="https://unpkg.com/react-input-autosize/dist/react-input-autosize.js"></script> <script src="https://unpkg.com/react-select/dist/react-select.js"></script> <link rel="stylesheet" href="https://unpkg.com/react-select/dist/react-select.css"> <div id='app'/> 

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

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