【发布时间】:2020-11-11 14:51:30
【问题描述】:
我正在尝试清除以前的状态,无论是单词还是错误,在每次新提交时都有一个值。这可能来自 handleSubmit 函数吗?我尝试通过在提交后将状态设置为空字符串来清除先前的状态,但不幸的是没有奏效。
state = {
word:'',
error: '',
};
handleChange = (event) => {
this.setState({word:event.target.value.toLowerCase()});
}
handleSubmit = (e) => {
e.preventDefault();
this.search(this.state.word)
this.setState({word: ''});
this.setState({error: ''});
}
search = async (word) => {
try{
const data = await MerriamAPI.getWordInfo(word);
console.log(data);
//&& word exists
if (data.length && data[0].fl && data[0].meta.stems && data[0].hwi.prs[0].mw && data[0].shortdef[0]){
console.log('A HERE')
this.props.handleUpdate({
word: word,
info: data,
versions: data[0].meta.stems,
shortdef: data[0].shortdef[0],
partOfSpeech: data[0].fl,
pronunciation: data[0].hwi.prs[0].mw,
});
this.props.setRedirect({
path: `/definition/${word}`,
});
}
else {
console.log('B HERE')
this.setState({error: 'Word Not Found'});
console.log(this.state.error)
}
}
catch{
this.props.setModal('Offline')
}
}
【问题讨论】:
标签: reactjs event-handling state submit