So I have two components... and I want them to render one after the other... THe code is below
var Recipe = React.createClass({
getInitialState: function(){
return {editing:false},
{titles: []}
},
edit: function (){
this.setState({editing:true});
},
remove: function(){
this.props.deleteFromBoard(this.props.index)
},
save: function (){
this.props.updateRecipeText(this.refs.newText.value,this.props.index)
this.setState({editing:false});
},
renderNormal: function() {
return (
<div className="recipeContainer">
<div>{this.props.children}</div>
<button onClick ={this.edit} className ="button-primary">Edit</button>
<button onClick ={this.remove} className ="button-remove">Remove</button>
</div>
);
},
renderForm: function() {
return (
<div className="recipeContainer">
<textArea ref="newText" defaultValue ={this.props.children}></textArea>
<button onClick ={this.save} className ="button-danger">Save</button>
</div>
);
},
render: function() {
if(this.state.editing){
return this.renderForm();
}
else{
return this.renderNormal();
}
}
});
var RecipeTitle = React.createClass({
getInitialState: function(){
return {editingTitle:false}
},
edit: function (){
this.setState({editingTitle:true});
},
remove: function(){
this.props.deleteFromBoard(this.props.index)
},
save: function (){
this.props.updateTitle(this.refs.newTextTitle.value,this.props.index)
this.setState({editingTitle:false});
},
renderTitleNormal: function(){
return(
<div>
<h2>{this.props.children}</h2>
<button onClick = {this.edit}>Edit</button>
</div>
)
},
renderTitleForm: function() {
return (
<div>
<textArea ref="newTextTitle" ></textArea>
<button onClick ={this.save} className ="button-danger">Save</button>
</div>
);
},
render: function() {
if(this.state.editingTitle){
return this.renderTitleForm();
} else{
return this.renderTitleNormal();
}
}
});
var Board = React.createClass({
getInitialState: function () {
return {
recipes: [
],
titles: [
]
}
},
add: function(text,title){
var arr = this.state.recipes;
arr.push(text);
this.setState({recipes: arr})
var arrTitle = this.state.titles;
arrTitle.push("Title");
this.setState({titles: arrTitle})
},
removeRecipe: function (i) {
var arr = this.state.recipes;
console.log(arr);
arr.splice(i,1);
this.setState({recipes: arr})
},
removeTitle: function (i) {
var arr = this.state.titles;
arr.splice(i,1);
this.setState({titles: arr})
},
updateRecipe: function (newText, i) {
var arr = this.state.recipes;
arr[i]=newText;
this.setState({recipes: arr})
},
eachTitle: function (title, i){
return (<div><RecipeTitle key={i} index={i} updateTitleText={this.updateTitle} >{title}</RecipeTitle></div>);
},
updateTitleText: function (newTitle, i) {
var arr = this.state.titles;
arr[i]=newTitle;
this.setState({titles: arr})
},
eachRecipe:
function (text, i){
return (<div><Recipe key={i} index={i} updateRecipeText={this.updateRecipe} deleteFromBoard={this.removeRecipe}>{text}</Recipe></div>);
},
eachTitle:
function (title, i){
return (<div><RecipeTitle key={i} index={i} updateTitle ={this.updateTitleText} deleteFromBoard={this.removeRecipe}>{title}</RecipeTitle></div>);
},
render: function () {
return(
<div>
<button onClick={this.add.bind(null,'Default Text')}>Add New</button>
<div >
{this.state.titles.map(this.eachTitle)}
{this.state.recipes.map(this.eachRecipe)}
</div>
</div>
);
}
});
ReactDOM.render(<Board />
, document.getElementById("app"));
Basically its a recipe title followed by a recipe. The problem is once I render a few what happens is that all the titles go together and not with each recipe eg... I want it like this..
Chicken Reicpe
Recipe here
Beef Recipe
Recipe Here
Cococnut Recipe
Recipe here
Instead its like this.
Chicken Recipe
Beef Recipe
Coconut Recipe
Recipe
Recipe
Recipe
How can I render them together? If you need more code or clarification let me know. Any suggestions to improve code? If it's too long to read let me know too. Thanks,
<Recipe />component that renders the title and recipe.