gpt4 book ai didi

javascript - 如何在表单提交后清除输入(React)

转载 作者:行者123 更新时间:2023-12-01 01:04:11 24 4
gpt4 key购买 nike

我有一个搜索输入,我想在提交值并使用该值执行搜索后清除该输入。在类似的问题中,建议将输入值的状态设置为 '' ,但我认为这就是我尝试过的,但它没有做任何事情。

我的应用程序中只有父组件和子组件。父组件有一个搜索笑话的方法 (searchJokes),并通过 onFormSubmit={this.searchJokes 作为名称不同的 prop 传递给组件实例中的子组件}。在子组件中,当用户在搜索输入中输入内容时,其 event.target.value 会通过 onChange={e => props.onInputChange(e.target.value) 传递} 对应父 prop 中的 onSearchChange 方法,该值用于更新 searchTerm 的状态。

我将 searchTerm: '' 添加到 searchJokes 方法的末尾,该方法根据搜索词获取搜索,如下面的父组件代码所示。

父组件:

class App extends Component {
constructor() {
super();

this.state = {
searchTerm: '',
jokes: [],
isFetchingJokes: false,
isSearch: false
};

this.onSearchChange = this.onSearchChange.bind(this);
this.randomizeJokes = this.randomizeJokes.bind(this);
this.searchJokes = this.searchJokes.bind(this);
}

randomizeJokes() {
this.setState({
isFetchingJokes: true,
isSearch: false
});

fetch(
'https://icanhazdadjoke.com/',
{
method: 'GET',
headers: {
Accept: 'application/json'
}
})
.then(response => response.json())
.then(json => {
let joke = json.joke;
this.setState({
joke,
isFetchingJokes: false
});
});
}

searchJokes(limit = 15) {
// If nothing entered, user gets "Please fill out this field" message due to "required" attribute on input element
if (this.state.searchTerm !== '') {
this.setState({
isFetchingJokes: true,
isSearch: true
});

fetch(
`https://icanhazdadjoke.com/search?term=${
this.state.searchTerm
}&limit=${limit}`,
{
method: 'GET',
headers: {
Accept: 'application/json'
}
})
.then(response => response.json())
.then(json => {
let jokes = json.results;
this.setState({
jokes,
isFetchingJokes: false,
searchTerm: '' // <-- DOESN'T CLEAR INPUT
});
});
}
}

onSearchChange(value) {
this.setState({ searchTerm: value });
}

jokeRender() {
return (
<div>
{this.state.isSearch ?
<ul>{this.state.jokes.map(item => <li key={item.id}>{item.joke}</li>)}
</ul> : <p className="random-joke">{this.state.joke}</p>}
</div>
);
}

render() {
return (
<div>
<h1>Dad Jokes</h1>
<RetrievalForm
onFormSubmit={this.searchJokes}
onInputChange={this.onSearchChange}
isSearching={this.state.isFetchingJokes}
onRandomize={this.randomizeJokes}
/>

{this.state.isFetchingJokes ? <p className="searching-message">Searching for jokes...</p> : this.jokeRender()}
</div>
);
};
}

子组件:

const RetrievalForm = props => {
const onSubmit = e => {
// Prevents GET request/page refresh on submit
e.preventDefault();
props.onFormSubmit();
};

return (
<>
<form onSubmit={onSubmit}>
<input
type="text"
placeholder="Enter search term..."
onChange={e => props.onInputChange(e.target.value)}
required
/>
<div>
{/* Specifying type here since it's good practice; different browsers may use default types for buttons */}
<button type="submit" disabled={props.isSearching}>Search</button>
{/* type="button" stops input validation message from being displayed (on Firefox) when randomize button is clicked without anything entered */}
<button type="button" onClick={props.onRandomize} disabled={props.isSearching} className="randomize-button">
Randomize
</button>
</div>
</form>
</>
);
};

任何帮助将不胜感激。

最佳答案

您需要将 searchTerm 传递给 RetrievalForm,并在该输入中设置 value={searchTerm},以便它的值将绑定(bind)到该状态。

关于javascript - 如何在表单提交后清除输入(React),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55787972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com