gpt4 book ai didi

javascript - ReactJs 如何在初始渲染后重新调用 API?

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

我有一个带有搜索栏的主页组件。

const Intro = (props) => {

return (
<Searchbar
onSubmit={props.onSubmit}
onChange={props.onChange}
value={props.value}
header='Nutrion'>
</Searchbar>
)
}

搜索栏是一个受控组件。它获得传递的事件处理程序

handleSubmit(e) {
e.preventDefault()
this.setState({
food: this.state.value,
submitted: true
})
}

如果搜索栏中的值被提交,它会设置 statefood: this.state.valuesubscribed: true

submitted && <Redirect to={{
pathname: `/search/results`,
search: `?food=${food}`,
}} />

Subscribed true 触发到结果组件的重定向,并在搜索栏中传递带有提交食物的查询字符串。

   componentDidMount() {
console.log('hiii')
const food = this.state.food || queryString.parse(this.props.location.search).food
fetchRecipes(food).then(recipes => {
if (recipes === null) {
return this.setState (
{
error: 'Server failed to respond. Please try again.',
loading: false
})
}

this.setState( {
error: null,
recipes: recipes,
loading: false,
isFetched: true
})
})

}

问题就在这里。这是重定向到 class Results 内的 ComponentDidMount()。它接受我们之前传递的查询字符串并解析它。其结果用于 API 请求,返回的数据将传递到 this.state.recipes。一切正常。数据被传递到 recipeList

{!this.state.loading && <Recipelist recipes={this.state.recipes} />}

但这仅适用于初始渲染。如果我通过在结果中的(另一个)搜索栏中提交值来更改 this.state.food,它不会重新请求 API 数据并使用新的食谱更新 RecipeList。如何发出新的 API 请求,将每个项目的新值提交到 this.state.food 并重新渲染 RecipeList?

我已在下面发布了相关文件的链接:

import React from 'react'
import { Redirect } from 'react-router-dom'
import Searchbar from './../ui/Searchbar'

import {
Jumbotron,
// PageHeader
} from 'react-bootstrap'

const Intro = (props) => {

return (
<Searchbar
onSubmit={props.onSubmit}
onChange={props.onChange}
value={props.value}
header='Nutrion'>
</Searchbar>
)
}

class Home extends React.Component {

constructor(props) {
super(props)
this.state = {
submitted: false,
food: '',
value: ''
}

this.handleSubmit = this.handleSubmit.bind(this)
this.handleChange = this.handleChange.bind(this)
}
handleChange(e) {
this.setState({ value: e.target.value })
}

handleSubmit(e) {
e.preventDefault()
this.setState({
food: this.state.value,
submitted: true
})
}

render() {
// REMINDER: toegang tot path is via this.props.match => match.url object
const submitted = this.state.submitted
const food = this.state.food

return (

<Jumbotron>
{
!submitted &&
<Intro onSubmit={this.handleSubmit}
onChange={this.handleChange}
value={this.state.value}/>
}
{
submitted &&
<Redirect to={{
pathname: `/search/results`,
search: `?food=${food}`,
}} />
}
</Jumbotron>

)
}
}

export default Home

import React, {Component} from 'react'
import {Jumbotron} from 'react-bootstrap'
import Searchbar from './../../ui/Searchbar'
import { fetchRecipes } from './../../utils/api'
import queryString from 'query-string'
import Recipelist from './Recipelist'

class Results extends Component {

constructor(props) {
super(props)
this.state = {
value: '',
food: '',
recipes: null,
isFetched: false,
error: null,
loading: true
}

this.handleChange = this.handleChange.bind(this)
this.handleSubmit = this.handleSubmit.bind(this)
}

handleChange(e) {
this.setState({ value: e.target.value })
}

componentDidMount() {
console.log('hiii')
const food = this.state.food || queryString.parse(this.props.location.search).food
fetchRecipes(food).then(recipes => {
if (recipes === null) {
return this.setState (
{
error: 'Server failed to respond. Please try again.',
loading: false
})
}

this.setState( {
error: null,
recipes: recipes,
loading: false,
isFetched: true
})
})

}


handleSubmit(e) {
e.preventDefault()
this.setState( {
food: this.state.value
})
}

render(){

if (this.state.loading) {
return <p> Loading ... </p>
}
if (this.state.error) {
return (
<div>
<p>{this.state.error}</p>
{/*<Link to='/'>try again</Link>*/}
</div>
)
}
return (
<div>
<Jumbotron>
<Searchbar
value={this.state.value}
onSubmit={this.handleSubmit}
onChange={this.handleChange}/>
</Jumbotron>
{!this.state.loading && <Recipelist recipes={this.state.recipes} />}
</div>
)
}
}


export default Results

{
"name": "nutrion",
"version": "0.1.0",
"private": true,
"dependencies": {
"normalize-css": "^2.3.1",
"prop-types": "^15.5.10",
"query-string": "^4.3.4",
"react": "^15.5.4",
"react-bootstrap": "^0.31.0",
"react-dom": "^15.5.4",
"react-router-dom": "^4.1.1",
"semantic-ui-react": "^0.68.5"
},
"devDependencies": {
"react-scripts": "1.0.7"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}

最佳答案

您可以在handleSubmit 中调用 fetchRecipes(food)

handleSubmit(e) {
e.preventDefault();
let recipes = await = fetchRecipes(food);
let food = e.target.value;
this.setState( {
food, recipes
});
}

关于javascript - ReactJs 如何在初始渲染后重新调用 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44607353/

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