0

Currently I am working on this FCC project: https://www.freecodecamp.com/challenges/build-a-recipe-box

As of now, I was able to implement on adding new recipes to the list.

However, I am having hard to implement on how to edit/delete per each recipe item list. For now, I just want to focus on how to DELETE per item.

The way I am displaying the recipe list is in the RecipeBox Container, where I am using map function to iteratively rendering each of them from the app's state, as well as rendering the buttons EDIT and DELETE.

But I cant seem to attach an action to it. I get the following error:

Uncaught TypeError: Cannot read property 'props' of undefined

RecipeBox Container:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { ListGroup, ListGroupItem, Panel, Button, Modals } from 'react-bootstrap'
import { bindActionCreators } from 'redux';
import { deleteRecipe } from '../actions/index';

class RecipeBox extends Component {
  constructor(props){
    super(props);
    this.state = {
      open: false
    };

  }
  renderRecipeList(recipeItem,index){
    const recipe = recipeItem.recipe;
    const ingredients = recipeItem.ingredients;
    return(
      <div key={index}>
        <Panel bsStyle="primary" collapsible header={<h3>{recipe}</h3>}>
          <ListGroup >
            <ListGroupItem  header="Ingredients"></ListGroupItem>
            {ingredients.map(function(ingredient,index){
              return <ListGroupItem key={index}>{ingredient}</ListGroupItem>;
            })}
            <ListGroupItem>
              <Button
                onClick={this.props.deleteRecipe(recipeItem)}
                bsStyle="danger">Delete
              </Button>
              <Button
                onClick={() => console.log('EDIT!')}
                bsStyle="info">Edit
              </Button>
            </ListGroupItem>
          </ListGroup>
        </Panel>
      </div>
    )
  }
  render(){
    return(
      <div className="container">
        <div className='panel-group'>
          {this.props.addRecipe.map(this.renderRecipeList)}
        </div>
      </div>
    )
  }
}

function mapStateToProps(state) {
  return {
    addRecipe : state.addRecipe
  };
}

function mapDispatchToProps(dispatch){
  return bindActionCreators({deleteRecipe}, dispatch)
}

export default connect(mapStateToProps,mapDispatchToProps)(RecipeBox);

This seems very trivial, but I keep hitting a roadblock..

1
  • can you show us your reducer code, so that we can check it? Commented Aug 28, 2016 at 20:05

2 Answers 2

1

Add this.renderRecipeList = this.renderRecipeList.bind(this) in the constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

Oops, forgot about binding it..Thanks a bunch!
1
 render(){
 return(
  <div className="container">
    <div className='panel-group'>
      {this.props.addRecipe.map(this.renderRecipeList.bind(this))}
    </div>
  </div>
 )
}

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.