Last active
January 22, 2017 13:55
-
-
Save borisd/c34013b2e11906df5096fbf774362678 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { render } from 'react-dom'; | |
import './index.css'; | |
const Recipe = ({ recipe }) => ( | |
<li>{ recipe }</li> | |
); | |
const Recipes = ({ recipes }) => ( | |
<ul> | |
{ recipes.map(recipe => <Recipe key={ recipe } recipe={ recipe } /> )} | |
</ul> | |
); | |
class AddRecipe extends React.Component { | |
render() { | |
return ( | |
<form onSubmit={ this.onSubmit.bind(this) }> | |
<input ref={ e => this.title = e } type="text"/> | |
<button>Add</button> | |
</form>) | |
} | |
onSubmit(e) { | |
e.preventDefault(); | |
this.props.cb(this.title.value); | |
this.title.value = ''; | |
} | |
} | |
class App extends React.Component { | |
constructor() { | |
super(); | |
this.state = { | |
recipes: ['Waffles', 'Omelette'] | |
}; | |
} | |
addRecipe(title) { | |
this.setState({ | |
recipes: this.state.recipes.concat(title) | |
}); | |
} | |
render() { | |
return ( | |
<div> | |
<h1>Recipes:</h1> | |
<Recipes recipes={ this.state.recipes }/> | |
<AddRecipe cb={ this.addRecipe.bind(this) }/> | |
</div> | |
); | |
} | |
} | |
render( | |
React.createElement(App), | |
document.getElementById('root') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment