[英]Can't type in my input since i've set its value to this.state in ReactJS
I'm facing some basic React problem. 我面临一些基本的React问题。
I made a basic text input with change handling and onKeyPress handling but since i made its value equal to this.state.currentSearchText, i can't type in it, everything is binded, and it's working on some other project. 我使用更改处理和onKeyPress处理进行了基本的文本输入,但是由于我将其值设置为等于this.state.currentSearchText,因此我无法输入该值,所有内容都已绑定,并且可以在其他项目上使用。 I need light.
我需要光。
My input : 我的输入:
<input type="text" value={this.state.currentSearchText} className="search-header input-text" placeholder={searchInputPlaceholder} onChange={this.handleChange} onKeyPress={(e) => this.handleKeyPress(e)} autoComplete="off"/>
My change handling func : 我的变更处理功能:
handleChange(event) {
this.setState({currentSearchText: event.target.value})
console.log(this.state.currentSearchText)
event.target.value.length === 0 ? this.displayHistory() : this.displaySearch()
this.timeoutGTM = setTimeout(() => {
pushGTM({
searchTerm: this.state.currentSearchText,
event: 'search_event'
})
}, 400)
}
My key press handling : 我的按键处理:
handleKeyPress(event) {
if (event.key === 'Enter' && this.state.currentSearchText.length > 0) {
this.context.router.push('/search?searchEntry=' + this.state.currentSearchText)
}
}
The state of my SearchBar component : 我的SearchBar组件的状态:
class SearchBar extends Component {
constructor(props) {
super(props)
this.state = {
currentSearchText: '',
resultsPosition: 0,
nbTotalResults: 0,
nbHistoryItems: 0,
viewMode: false,
lastKeyPressed: '',
showResultsThumbnail: false
}
this._handleKeyPress = this._handleKeyPress.bind(this)
this.handleChange = this.handleChange.bind(this)
this.handleKeyPress = this.handleKeyPress.bind(this)
this.hideSearchContent = this.hideSearchContent.bind(this)
ControllerShortcuts.setOnKeyboardKeyPress('/', () => {
this._refInputSearch.focus()
this.onFocus()
})
}
在输入字段中仅使用onChange
<input type="text" value={this.state.currentSearchText} className="search-header input-text" placeholder={searchInputPlaceholder} onChange={this.handleChange} autoComplete="off"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.