【问题标题】:ReactJS : How to properly handle data fetched from food2fork API?ReactJS:如何正确处理从 food2fork API 获取的数据?
【发布时间】:2019-03-24 21:28:20
【问题描述】:

我有一个用 ReactJS 构建的应用程序。它的目的是显示食谱,在 food2fork API 中搜索。 我对更新父组件的状态没有任何问题。单击应用程序中的“搜索”按钮后获取数据。 我的问题与将获取的数据作为道具发送给子组件并根据当前搜索正确显示收到的食谱有关。


handleChange 仅用于处理输入。

handleSearch 是我想使用按钮的“onClick”来显示从 API 获取的数据。

获取的食谱应该显示在结果组件中。

希望清楚:)


除了仅将状态作为来自父组件的道具传递并在子组件中使用它之外,我还尝试使用生命周期方法根据收到的道具更新子状态 - 也许我没有正确使用它们...

父组件:

import React, { Component } from 'react';
import Results from './Results';

class Recipes extends Component {
    constructor(props){
        super(props);
        this.state = {
            search: '',
            recipes: []
        }
    }
    handleChange=e=>{
        this.setState({
            search: e.target.value
        })
    }


    handleSearch =()=>{
            if(this.state.search !== ''){
                const url = `https://www.food2fork.com/api/search?key=367d2d744696f9edff53ec5b33a1ce64&q=${this.state.search}`

                fetch(url)
                        .then(data => data.json())
                        .then(jsonData => {
                            this.setState((jsonData)=> {return {
                                recipes: jsonData}
                            })
                        })

            } else {
                console.log('empty')
            }
    }

    render() {
        return (
            <Wrapper>
                <SearchBar 
                    value={this.state.search} 
                    type='search' 
                    onChange={this.handleChange}>
                </SearchBar>
                <SearchButton onClick={this.handleSearch}>SEARCH</SearchButton>

                <Results recipes={this.state.search}/>

            </Wrapper>
        );
    }
}

export default Recipes;

儿童组件“结果”应该接收更新的食谱列表作为道具并显示这些食谱。

import React from 'react';
import Recipe from './Recipe';

class Results extends React.Component {
    render(){
        return (
            <Container>
                <RecipesList>
                    {this.props.recipes.map(item => 
                            <Recipe 
                                f2fURL={item.f2f_url}
                                image={item.image_url}
                                publisher={item.publisher}
                                publisherURL={item.publisher_url}
                                recipeID={item.recipe_id}
                                source={item.source_url}
                                title={item.title}
                            />)}
                </RecipesList>
            </Container>
        );
    }
};

【问题讨论】:

  • 我认为您可能有错字。看起来您将 this.state.search 作为属性传递给子组件。你应该传入 this.state.recipes :)

标签: reactjs api asynchronous


【解决方案1】:

正如@yourfavoritedev 所提到的,您在结果道具上有一个错字。它应该是 recipes={this.state.recipes} 而不是 recipes={this.state.search}

你也应该改变:

this.setState((jsonData)=> {return {
                                recipes: jsonData}
                            })

为:

this.setState({ recipes: jsonData })

更新程序功能将是这样的(文档here):

(state, props) => stateChange

因此,您在 setState 上使用的 jsonData 实际上是之前的状态,而不是来自 api 调用的数据。

【讨论】:

  • 我认为这总结了我遇到的所有问题。非常感谢您的帮助。现在一切正常。 this.state.search 确实是一个错字:)。我想我是在尝试实施一项更改以使此请求生效之后才做到的。
【解决方案2】:

你的问题来了

this.setState((jsonData)=> {return {
    recipes: jsonData}
})

在您的 ajax 响应中。

改成

this.setState({recipes: jsonData});

这应该正确设置食谱对象。

【讨论】:

    猜你喜欢
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 2021-05-22
    • 1970-01-01
    • 2019-03-25
    • 2016-11-27
    相关资源
    最近更新 更多