简体   繁体   English

反应选择onChange不使用选项值

[英]React select onChange doesnt use the options value

Hello i have this select component 您好,我有这个选择组件

<select
    value={(this.state.selectedVersion)}
    disabled={(this.state.versionDisable)}
    onChange={this.handleVersionChange}
>
    <option disabled hidden>Välj version</option>
    {
        this.state.versions.map((object, i) => {
            return (
                <option key={i} value={object}>
                    {object.text}{object.lastModified}
                </option>
            )
        })
    }
</select>

here i want the onChange event to take the options value wich i have logged is the thing i want and use it as value but when i send it to my onChange handler 在这里,我希望onChange事件采用我已记录的选项值,这是我想要的东西并将其用作值,但是当我将其发送到我的onChange处理程序时

handleVersionChange(event){
    console.log(event);
    this.setState({selectedVersion: event.target.value});
    console.log(this.state.selectedVersion);
}

The first console.log that logs the event that got sent i get some proxy object that i duno what it is or were it is from. 记录发送事件的第一个console.log我得到一些代理对象,我不知道它是什么或来自什么。 I duno if i get this problem sincei create the options with the map. 如果我遇到这个问题,我不知道,因为我用地图创建了选项。 Anyone know what i am doing wrong? 有人知道我在做什么错吗?

I still think the problem is setting the value attribute on the option element as an Object. 我仍然认为问题在于将option元素上的value属性设置为Object。 Consider using a string instead. 考虑使用字符串代替。 You can always reverse lookup a value in your change handler from the original array to get the full object. 您始终可以在更改处理程序中从原始数组反向查找值以获取完整的对象。

In the following example I use an attribute I created called id to use as the select option value. 在下面的示例中,我使用创建的名为id的属性作为选择选项值。 This value can be any unique identifier in your object which is a simple type like string or number. 此值可以是对象中的任何唯一标识符,该标识符是简单类型,例如字符串或数字。 The same value can be used to reverse lookup the full object 相同的值可用于反向查询整个对象

Sample JS Fiddle 样本JS小提琴

 class SelectApp extends React.Component { constructor(props) { super(props) this.state = { items: [ { text: "Learn JavaScript", id: 1 }, { text: "Learn React", id: 2 }, { text: "Play around in JSFiddle", id: 3 }, { text: "Build something awesome", id: 4 } ] } selected:null } handleChange = (e)=>{ // Get the selected value which is a string var selectedId = e.target.value; console.log(selectedId); // Reverse look up on the array to find the full object var selected = this.state.items.find((d)=>(d.id==selectedId)); console.log(selected); // Update state with object this.setState({selected}); } render() { return ( <div> <select onChange={this.handleChange}> <option disabled selected>Select</option> {this.state.items.map((item, i) => ( <option key={i} value={item.id}>{item.text}</option> ))} </select> </div> ) } } ReactDOM.render(<SelectApp />, document.querySelector("#app")) 

Your handleVersionChange need to be a listen function. 您的handleVersionChange必须是一个侦听功能。 Replace by this : 替换为:

 handleVersionChange = (event) => {
     console.log(event);
     this.setState({selectedVersion: event.target.value});
     console.log(this.state.selectedVersion);
 }

Or in your constructor add : 或者在您的constructor添加:

this.handleVersionChange = this.handleVersionChange.bind(this)

Why do you put another pair of braces inside curly ones: {(this.state.selectedVersion)} ? 为什么{(this.state.selectedVersion)}在大括号内放另外一对大括号: {(this.state.selectedVersion)} Am I missing something: {this.state.selectedVersion} ? 我是否缺少某些内容: {this.state.selectedVersion}

Trying console logging in componentWillReceiveProps instead of inside your handle change function. 尝试在componentWillReceiveProps中而不是在句柄更改函数内部进行控制台日志记录。

SetState is asynchronous. SetState是异步的。

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

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