gpt4 book ai didi

reactjs - 在 React 中显示逗号分隔的数组元素

转载 作者:行者123 更新时间:2023-12-02 05:47:59 26 4
gpt4 key购买 nike

 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>
);
}

}

我的sample-recipe.js文件如下
module.exports = [ 
{
name : 'chicken',
ingredient : ['spinach','chillies']
}];

嗨,我是 React 的新手。我正在制作这个食谱书项目。
我想显示以空格或逗号分隔的成分。
现在它显示为“菠菜”。
这是使成分成为数组的正确方法吗?

最佳答案

由于成分是 array of strings您可以join它创建一个字符串并显示结果

{this.state.recipes.map(recipe => {
return ( <div key={recipe.name}>
<dt>{recipe.name}</dt>
<dd>{recipe.ingredient.join(",")}</dd>
<hr></hr>
</div>
)
})
}

关于reactjs - 在 React 中显示逗号分隔的数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43309773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com