为什么我的函数onInputBlur
被视为未定义?我想这是非常愚蠢的,但我已经尝试在构造函数和其他变体中进行绑定。
import React from 'react';
class StateSelect extends React.Component {
constructor(props) {
super(props)
this.state = {}
}
onInputBlur = (event) => {
let choiceType = event.target.id.toString()
let choice = event.target.value
console.log(this.props.state)
}
render() {
const { styles } = this.props
return (
<div style={styles.formInput}>
<select id="state" name="state" onChange={this.props.saveInputVal} onBlur={onInputBlur} style={styles.input}>
<option value disabled selected>State</option>
{Object.keys(States).map((key) => {
return ( <option key={key} value={key}>{States[key]}</option> )
})}
</select>
</div>
)
}
}
答案 0 :(得分:2)
您需要写onBlur={this.onInputBlur}
。您可以使用this
关键字
片段
render() {
const { styles } = this.props
return (
<div style={styles.formInput}>
<select id="state" name="state" onChange={this.props.saveInputVal} onBlur={this.onInputBlur} style={styles.input}>
<option value disabled selected>State</option>
{Object.keys(States).map((key) => {
return ( <option key={key} value={key}>{States[key]}</option> )
})}
</select>
</div>
)
}