我是超级新手,我正在努力让我的 onClick 工作。当页面呈现时,我看到它被调用(多次,取决于数组中的项目数)但不是点击。
const BlogPage = props => {
const posts = props.data.allContentfulPost.edges;
const postsStable = props.data.allContentfulPost.edges;
const categoriesStyle = {
marginBottom: '8px',
};
const filterCategories = category => {
console.log('here', category);
// this.posts = this.posts.filter(p => p.catergory === category);
};.
return (
<div style={categoriesStyle}>
{posts.map(({ node: c }) => (
<Badge onClick={filterCategories(c.category)}
key={c.id}
category={c.category}
color="#fff">
{c.category}
</Badge>
))}
</div>
);
};
我试过了:
onClick={() => filterCategories(c.category)}
和
onClick={() => {
filterCategories(c.category);
}}
const Badge = ({ children, category = 'Other', color = '#4a4a4a' }) => {
return (
<Container background={pillColor(category)} color={color}>
{children}
</Container>
);
};
Badge.propTypes = {
children: PropTypes.node.isRequired,
category: PropTypes.string,
color: PropTypes.string,
};
根据答案,问题似乎是 Badge 不是 HTML 元素。
斯蒂芬大帝
幕布斯7119047
相关分类