Javascript Can';t输入我的输入,因为i';我已在ReactJS中将其值设置为this.state
我面临一些基本的问题 我用change handling和onKeyPress handling做了一个基本的文本输入,但是因为我把它的值设为this.state.currentSearchText,所以我不能输入它,所有的东西都是绑定的,它正在其他项目中工作。我需要光 我的意见:Javascript Can';t输入我的输入,因为i';我已在ReactJS中将其值设置为this.state,javascript,reactjs,input,binding,Javascript,Reactjs,Input,Binding,我面临一些基本的问题 我用change handling和onKeyPress handling做了一个基本的文本输入,但是因为我把它的值设为this.state.currentSearchText,所以我不能输入它,所有的东西都是绑定的,它正在其他项目中工作。我需要光 我的意见: <input type="text" value={this.state.currentSearchText} className="search-header input-text" placeholder=
<input type="text" value={this.state.currentSearchText} className="search-header input-text" placeholder={searchInputPlaceholder} onChange={this.handleChange} onKeyPress={(e) => this.handleKeyPress(e)} autoComplete="off"/>
我的按键操作:
handleKeyPress(event) {
if (event.key === 'Enter' && this.state.currentSearchText.length > 0) {
this.context.router.push('/search?searchEntry=' + this.state.currentSearchText)
}
}
我的搜索栏组件的状态:
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"/>
handleChange函数中的console.log(event.target.value)是否记录了输入的值它是否记录了一个空格..阅读文档。。。setState不会立即影响状态。如果依赖已设置的状态,请使用它的回调参数。。。这告诉我要完全按照我的方式来做。This.timeoutGTM=setTimeout(()=>{pushGTM({searchTerm:This.state.currentSearchText,event:'search\u event'}),400)在这里做什么。我认为问题就在那里,因为如果我删除它并同时使用我们的代码,它似乎工作正常
<input type="text" value={this.state.currentSearchText} className="search-header input-text" placeholder={searchInputPlaceholder} onChange={this.handleChange} autoComplete="off"/>