私はreactJSでレシピボックスを構築しています。私の目的は、レシピのタイトルを記載したボタン内の成分を隠すことです。したがって、「チーズケーキ」というタイトルのボタンをクリックすると、それぞれの成分が表示されます。 「オンクリック(Onclick)」に関連しているため、これをやりなおさないとき、いつ通信するかのブール値は意味をなさない。しかし、私の成分データ(this.props.ingredients)がレシピエントボタンコンポーネントに関与していることを前提に、データをフェッチするときにこのアクションをどのように調整するかはわかりません。私は、タイトルボタン内の成分コンポーネントを再初期化しようとしましたが、データをマップしたときにrecipeTitleButton内で定義することができます。しかし、これはうまくいかず、きれいに感じられませんでした。とにかく、これは理にかなっていると思います。どんな助けでも大歓迎です。 おかげreactjsでデータをレンダリングするときにカプセル化されたコンポーネントの小道具を調整する
var recipes = [{
recipe_title: "Cheesecake",
ingredients: "cream cheese, graham crackers, butter, eggs"
}, {
recipe_title: "Lasagna",
ingredients: " ricotta cheese, ground beef, pasta shells, parsely"
}, {
recipe_title: "Spaghetti",
ingredients: "noodles, pasta sauce, ground beef"
}]
var RecipeTitleButton = React.createClass({
getInitialState: function() {
return {
showIngredients: false
}
},
onClick: function() {
this.setState({
showIngredients: true
})
},
render: function() {
<Ingredients ingredients={this.props.ingredients}/>
return (
<div>
<button type="button" className="recipe_title_button" class="btn btn=primary btn-lg">{this.props.recipe_title}</button>
{this.state.showIngredients ? <Ingredients/>: null}
</div>
)
}
})
var Ingredients = React.createClass({
render: function() {
return (
<div id="ingredients" className="recipe_title_ingredients">
{this.props.ingredients}
</div>
)
}
})
var MainRecipeDisplay = React.createClass({
getInitialState: function() {
return {
recipeDataObject: recipes
}
},
render: function() {
var Pages = this.state.recipeDataObject.map(function(recipeContents) {
<RecipeTitleButton recipe_title={recipeContents.recipe_title} ingredients={recipeContents.ingredients}/>
})
return (
<div>
{Pages}
</div>
)
}
})
ReactDOM.render(<MainRecipeDisplay/>, document.getElementById('content'))