javascript - 如何将状态数组传输到道具
问题描述
所以我有 2 个 React 文件,一个 App.js 和一个 category.js。当我将数组传输到 category.js 时,它会显示一大块文本,而不是将其拆分为不同的数组按钮。我尝试搜索,但我不明白该怎么做。有人可以解释一下吗?
这是类应用程序
class App extends React.Component {
Object.keys(data.categories).forEach((category,index) => {
arr.push(data.categories[index].name);
})
render() {
return (
<div>
<Categories
categoryName = { this.state.categoryName }
/>
</div>
这是使用道具的类别
const Categories = props => (
<div>
<br />
{ props.categoryName && <button> { props.categoryName } </button>}
</div>
);
解决方案
你需要映射你的categoryName
数组
const Categories = props => (
<div>
<br />
{ props.categoryName &&
props.categoryName.map(name => <button>{name}</button>)}
</div>
);
推荐阅读
- php - Laravel 5.8:beberlei/assert/lib/Assert/functions.php 返回类型产生错误
- swift - 如何将方法标记为“必需”
- python - 在 Pandas Dataframe 中为特定列查找其他值之间的值
- batch-file - 如何将多个变量从 VBS 传递到批处理脚本?
- sql - 将非格式化 CSV 导入 Postgres 数据库
- mysql - 调用 MySQL JSON 函数,传入表示数字属性的路径
- c# - 等待/异步仍然令人困惑
- c# - ASP.NET Core Web API & Entity Framework Core;为什么我会收到此错误?
- django - Django:检查用户是否喜欢模板中的帖子和相应的评论
- android - 在 FloatingActionButton 中使用 Material design 图标库