简体   繁体   English

将 function 值返回给反应组件

[英]Returning a function value to a react Component

What I'm trying to do is to pass a result of an API call to a React Component and create a List from it.我想要做的是将 API 调用的结果传递给 React 组件并从中创建一个列表。

What I dont know is how to pass value inside a function to the component?我不知道如何将 function 中的值传递给组件? Do I need to have state for it?我需要 state 吗?

searchMealsHandler(event) {
    if (event.target.value.length > 2) {
        let findMeals = fetch(`https://api.edamam.com/api/food-database/v2/parser?ingr=${event.target.value}&app_id=${appID}&app_key=${appKey}`)
            .then(res => res.json())
            .then(result => result.hints);

        return findMeals;
    }
}

render() {
    return (
        <div>
            <SearchMeal search={this.searchMealsHandler} />
            <MealList list="how to pass it here" />
            <SelectedMeals />
        </div>
    )
}

Yes you need to create state for your list, and then update it with setState after fetch finished是的,您需要为您的列表创建 state,然后在fetch完成后使用setState更新它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM