0

I am very new to react and I am trying to bring in data from a food2fork api but I am getting the error

TypeError: Cannot read property 'map' of undefined

if i undo this code, the error was none. this code is main place to occur error

  {recipes.map (recipe => {
    return <Recipe key={recipe.recipe_id} recipe={recipe}
    />;
  })}

Full Page of RecipeList.js

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

export default class RecipeList extends Component {
    render() {
        const { recipes } = this.props;
        return (
            <React.Fragment>
                <RecipeSearch />
                <h1>Recipe List</h1>
                <div className="container my-5">
                    {/* title */}
                    <div className="row">
                        <div className="col-10 mx-auto col-md-6 text-center text-uppercase mb-3">
                            <h1 className="text-slanted">Recipe List</h1> 
                        </div>
                    </div>
                    {/* end title */}
                    <div className="row">
                        {recipes.map (recipe => {
                            return <Recipe key={recipe.recipe_id} recipe={recipe}
                            />;
                        })}
                    </div>
                </div>

                <Recipe />
            </React.Fragment>
        );
    }
}

In App.js:

render(){
    console.log(this.state.recipes);

    return (
      <React.Fragment>
        <RecipeList />
        <RecipeDetails />
      </React.Fragment>
    );
 }
10
  • 1
    this.props.recipes is undefined. Please add the script where you're importing RecipeList component. Commented Nov 2, 2019 at 20:48
  • i tried as u say change this.props To this.props.recipes , but it is still showing error Commented Nov 2, 2019 at 20:54
  • That changes makes no sense since you're already destructuring props. What I mean is to edit your post and add the part of your code that calls your component, like this <RecipeList .../> Commented Nov 2, 2019 at 20:57
  • can u add the code of the component that is using this component? Commented Nov 2, 2019 at 21:03
  • u mean App.js or other js files in component folder it is need to be add Commented Nov 2, 2019 at 21:06

2 Answers 2

1

I am taking it that you are keeping the recipes data in this.state.recipes in App.js

You need to pass it as props to <RecipeList component to use it as props there.

So in your App.js just do this

render(){
  console.log(this.state.recipes);

  return (
    <React.Fragment>
      <RecipeList recipes={this.state.recipes}  />
      <RecipeDetails />
    </React.Fragment>
  );
}
Sign up to request clarification or add additional context in comments.

Comments

1

First, you are not passing any prop to RecipeList.
Change this in App.js

 return (
      <React.Fragment>
        <RecipeList recipes={this.state.recipes} />
        <RecipeDetails />
      </React.Fragment>
    );

Second, in RecipeList.js, render it only when the async function completes.
Change:

{recipes.map (recipe => {
  return <Recipe key={recipe.recipe_id} recipe={recipe}
    />;
  })}

To:

{recipes ? recipes.map (recipe => {
  return <Recipe key={recipe.recipe_id} recipe={recipe}
    />;
  }) : null
}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.