我想在這樣一個搜索輸入使用onSearch事件:陣營未知道具「onSearch」
render(){
return(
<input type="search" onSearch={() => console.warn('search') } />
);
}
,但我得到了unknown prop error,如何解決它的任何想法?
我想創建一個自動完成輸入,當用戶點擊鍵盤上的「搜索」圖標時觸發一個事件。
編輯:
onSearch是不是一個標準的屬性,所以我想反應過來不會支持它。由於onSearch是一樣的擊打回車鍵,使用onkeypress事件將做的工作:
render() {
return (
<main>
<input onKeyPress={ this.onKeyPress } type="search" list="options"/>
<datalist id="options">
{ OPTS.map(item => <option value={ item.nm } key={ item.id } data-id={ item.id } />) }
</datalist>
</main>
);
}
onKeyPress(event) {
if (event.key === 'Enter') {
const id = document.querySelector(`#options option[value="${event.target.value}"]`).dataset.id;
if (id) {
console.warn('user search', id);
}
}
}
嘗試使用'onSubmit'或'onChange'而不是'onSearch'。 –