Reputation: 2326
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..
Upvotes: 0
Views: 876
Reputation: 1593
render(){
return(
<div className="container">
<div className='panel-group'>
{this.props.addRecipe.map(this.renderRecipeList.bind(this))}
</div>
</div>
)
}
Upvotes: 1
Reputation: 702
Add this.renderRecipeList = this.renderRecipeList.bind(this)
in the constructor.
Upvotes: 1