繁体   English   中英

React / Redux:需要帮助来渲染API响应

[英]React/Redux: Need help render API Response

我正在开发食谱应用程序。 我正在使用Yummly API,但正在收到响应,但是由于如何响应是一个包含一系列食谱的对象,我对如何呈现从API返回的数据感到困惑。 当我尝试渲染数组时,出现此错误:

未捕获(承诺中)错误:对象作为React子对象无效(找到:带有键{imageUrlsBySize,sourceDisplayName,Ingredients,id,smallImageUrls,recipeName,totalTimeInSeconds,attribute,flavors,rating}的对象)。 如果要渲染子级集合,请改用数组。

链接到API响应的图像:

API “匹配”中的对象是我要在组件中呈现的部分

Action.js

import Axios from 'axios';
import {LOOK_UP_RECIPE`enter code here`} from './types';

 const API_ID = '########';
 const API_KEY = '######';
 const ROOT_LOOK_UP_URL = `http://api.yummly.com/v1/api/recipes? 
 _app_id=${API_ID}&_app_key=${API_KEY}`


export function lookuprecipesYummly(ingredients) {
 const yummlyurl =`${ROOT_LOOK_UP_URL}&q=${ingredients}`; 
 const request = Axios.get(yummlyurl);

return {
    type: LOOK_UP_RECIPE,
    payload: request
};
}

Reducer.js

import { LOOK_UP_RECIPE } from '../actions/types'
export default function(state = [], action) {
 console.log(action)
 switch (action.type){
    case LOOK_UP_RECIPE:
        return [ action.payload.data, ...state ];
 default:
    return state;
 }
}

零件:

    import _ from "lodash";
    import React, {Component} from 'react';
    import { connect } from 'react-redux';

class RecipeList extends Component {

 renderRecipe(recipeData) {
    return (
        <tr key={0}>
            <td key={1}>{recipeData.matches}</td>
        </tr>
    )
}

render() {

    return(
        <table>
            <thead>
                <tr key={1}>
                    <th>Recipe</th>
                </tr>
            </thead>
            <tbody>
                {this.props.recipes.map(this.renderRecipe)}
            </tbody>
        </table>
    )
}
}


function mapStateToProps({recipes}) {
   return {
       recipes
      }
};


export default connect(mapStateToProps)(RecipeList);

在此处输入图片说明

您需要在JSX中使用每个配方的数据。 这是如何填充表行的示例:

import _ from "lodash";
import React, {Component} from 'react';
import { connect } from 'react-redux';

class RecipeList extends Component {

    renderRecipe(recipe) {
        return (
            <tr key={recipe.id}>
                <td>{recipe.recipeName}</td>
                <td>{recipe.rating}</td>
                <td>{recipe.attributes.course[0]}</td>
                <td>{recipe.ingredients.join(', ')}</td>
            </tr>
        )
    }

    render() {

        return(
            <table>
                <thead>
                    <tr>
                        <th>Recipe</th>
                        <th>Rating</th>
                        <th>Course</th>
                        <th>Ingredients</th>
                    </tr>
                </thead>
                <tbody>
                    {this.props.recipes.matches.map(this.renderRecipe)}
                </tbody>
            </table>
        )
    }
}


function mapStateToProps({recipes}) {
    return { recipes }
};


export default connect(mapStateToProps)(RecipeList);

暂无
暂无

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

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