[英]I want the combined functioanlity of ComponentDidMount and ComponentDidUpdate in React js
I am fetching API data using axios and that API has query parameter (eg: searchQuery, which is defined in state).我正在使用 axios 获取 API 数据,并且 API 具有查询参数(例如:searchQuery,在状态中定义)。 I have declared it's initial value in state and axios call in componentDidUpdate.
我已经在 state 和 axios 调用 componentDidUpdate 中声明了它的初始值。 Now I want my user to input the data in the form that on submit will change "seachQuery" in state. Here comes the problem, I want to display the initial results with the provided value and future results according to the user's input but it doesn't happen.
现在我希望我的用户以提交时将在 state 中更改“seachQuery”的形式输入数据。问题来了,我想根据用户的输入显示具有提供的值和未来结果的初始结果,但它没有发生。 Initial results did don't show up because componentDidUpdate will be called only after updating.
初始结果没有显示,因为 componentDidUpdate 只会在更新后调用。 And if I made axios call in componentDidMount, results according to the user input don't appear.
如果我在 componentDidMount 中进行了 axios 调用,则不会显示根据用户输入的结果。 I WANT TO DO IT USING CLASS BASED COMPONENTS ONLY.
我只想使用基于 CLASS 的组件来做这件事。
constructor(props) {
super(props)
this.state = {
searchResult: [],
formInput: "",
searchQuery: "chicken",
}
}
componentDidUpdate(){
axios.get(`https://api.edamam.com/search?
q=${this.state.searchQuery}&app_id=${this.state.APP_ID}&app_key=${this.state.APP_KEY}`)
.then(res=>
this.setState(prevState =>({searchResult: prevState.searchResult.concat(res.data.hits)}))
)
}
onChangeHandler = (e) => {
this.setState({
formInput: e.target.value
})
console.log(e.target.value)
}
onSubmitHandler = (e) => {
e.preventDefault()
this.setState({
searchQuery: this.state.formInput
})
// console.log(this.state.searchQuery)
// setformInput("")
}
render() {
return (
<div className="App">
<form className="search-form" onSubmit={this.onSubmitHandler}>
<input className="search-bar" type="text" onChange={this.onChangeHandler} />
<button className="search-button" type="submit">SEARCH</button>
</form>
<div className="recipes">
{this.state.searchResult.map(singleRecipe =>
<Recipe key={singleRecipe.recipe.label} title={singleRecipe.recipe.label} calories={singleRecipe.recipe.calories} image={singleRecipe.recipe.image} ingredients={singleRecipe.recipe.ingredients}/>
)}
</div>
</div>
);
}
}
export default App```
I think you can set the state(eg searchQuery) in componentDidMount.我认为您可以在 componentDidMount 中设置状态(例如 searchQuery)。
componentDidMount() {
this.setState({searchQuery: 'checken'});
}
Why are you setting formInput to user input and searchQuery to formInput onSubmit?为什么将 formInput 设置为用户输入并将 searchQuery 设置为 formInput onSubmit? Just set searchQuery to user input onChange.
只需将 searchQuery 设置为用户输入 onChange。
onChangeHandler = (e) => {
this.setState({
searchQuery: e.target.value
});
console.log(e.target.value);
}
And fetch data in a function, use it on componentDidMount and onSubmit.并在 function 中获取数据,在 componentDidMount 和 onSubmit 上使用它。
getData = () => {
axios.get(`https://api.edamam.com/search? q=${this.state.searchQuery}&app_id=${this.state.APP_ID}&app_key=${this.state.APP_KEY}`)
.then(res =>
this.setState(prevState => ({ searchResult: prevState.searchResult.concat(res.data.hits) }))
);
}
componentDidMount() {
this.getData();
}
onSubmitHandler = (e) => {
e.preventDefault();
this.getData();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.