class App extends Component {
constructor(props){
super(props);
this.state={ recipes :[] }
this.addRecipe=this.addRecipe.bind(this);
}
addRecipe (recipe) {
console.log({...recipe})
this.setState({
recipes: [...this.state.recipes, recipe]
});
}
componentWillMount(){
this.setState({
recipes : require('./sample-recipes')
});
}
render() {
return (
<div className="App">
<h2>Welcome to the Recipe Book</h2>
<dl>
{this.state.recipes.map(recipe => {
return (<div key={recipe.name}>
<dt>{recipe.name}</dt>
<dd>{recipe.ingredient}</dd>
<hr></hr>
</div>
)
})
}
</dl>
<button className="addButton" onClick={() =>
{this.setState({ display: !this.state.display })}}>
Add Recipe
</button>
<AddRecipe addRecipe={this.addRecipe}
display={this.state.display} />
</div>
);
}
}
我的採樣recipe.js文件如下顯示陣列反應
module.exports = [
{
name : 'chicken',
ingredient : ['spinach','chillies']
}];
如何顯示通過空間或逗號分隔的成分。現在它顯示爲「spinachchillies」。 這是製作成分數組的正確方法嗎?
我也想學反應......是什麼涵蓋所有重要主題的最佳來源... –