EDIT 2: Respond 0.14.x
Now you can define functional components of an upright state for components that do not require complex intercepts of life cycle events or internal state
const PieceList = ({pieces, onDeletePiece2}) => { if (!onDeletePiece2) return; return ( <div className="piecesTable"> {pieces.map(x => ( <Pieces pieceData={x} onDeletePiece3={onDeletePiece2}> ))} </div> ); };
EDIT 1: ES6
As ES6 continues to become more visible, you can also avoid nitpicky problems by using the function with the ES6 arrow .
class PieceList extends React.Component { renderPiece(piece) { return <Piece pieceData={piece} onDeletePiece3={this.props.onDeletePiece2} />; } render() { if (!this.props.onDeletePiece2) return; return ( <div className="piecesTable"> {this.props.pieces.map(piece => this.renderPiece(piece))} <div> ); } }
To run it in most environments, you need to โblockโ it using something like babel.js
The quick answer is that you need to bind the correct this to the map callback, passing this as the second arg
this.props.pieces.map(..., this);
This may be the best way to write your tho component
var PieceList = React.createClass({ renderPiece: function(piece) { return <Piece pieceData={piece} onDeletePiece3={this.props.onDeletePiece2} />; }, render: function() { if (!this.props.onDeletePiece2) return; return ( <div className="piecesTable"> {this.props.pieces.map(this.renderPiece, this)} </div> ); } });
Regarding your comment about map
var x = {a: 1, b: 2}; ['a', 'b'].map(function(key) {
Will output
// "1" // "2"
Notice how the second argument to map can set the context for the function. When you call this inside a function, it will be equal to the second variable that was sent to map .
These are the basics of JavaScript, and you should definitely read more here.