Reactjs 向作为道具传递的组件添加道具
我正在尝试呈现项目列表。我的父组件定义要使用的renderItem组件 我是否可以向该组件传递其他道具Reactjs 向作为道具传递的组件添加道具,reactjs,Reactjs,我正在尝试呈现项目列表。我的父组件定义要使用的renderItem组件 我是否可以向该组件传递其他道具 <Route exact path="/users/:uid/clients" component={Clients} /> const ParentComponent = ({ match }) => { return ( <Component renderItem={SimpleCard} /> ); }; // Co
<Route exact path="/users/:uid/clients" component={Clients} />
const ParentComponent = ({ match }) => {
return (
<Component
renderItem={SimpleCard}
/>
);
};
// Component
...
renderList = list => {
const RenderItem = this.props.renderItem;
return list.map(item => {
return <RenderItem item={item} />;
});
}; // BUSTED
常量ParentComponent=({match})=>{
返回(
);
};
//组成部分
...
renderList=list=>{
const RenderItem=this.props.RenderItem;
返回列表.map(项=>{
返回;
});
}; // 破坏
渲染的RenderItem(又名SimpleCard)组件从不接收“项”属性,它是未定义的
// SimpleCard
export default function SimpleCard({ item, onClick}) {
const classes = useStyles();
return (
<Card className={classes.card} onClick={() => onClick(item)}>
<CardActionArea>
<CardContent>
<Typography gutterBottom variant="h5" component="h2">
{item && item.name}
</Typography>
{item && item.description && (
<Typography variant="body2" color="textSecondary" component="p">
{item && item.description}
</Typography>
)}
</CardContent>
</CardActionArea>
<CardActions>
<Button size="small" color="primary" onClick={() => onClick(item)}>
Details
</Button>
</CardActions>
</Card>
);
}
//SimpleCard
导出默认函数SimpleCard({item,onClick}){
const classes=useStyles();
返回(
onClick(项目)}>
{item&&item.name}
{item&&item.description&&(
{item&&item.description}
)}
onClick(项目)}>
细节
);
}
是的,我相信这是可能的,就像您可以使用react router
渲染组件以通过这样的道具一样
<Route path="/somepath/" component={MyComponent} />
<Route path="/someotherpath/" render={() => <MyOtherComponent props={"coolprop"}
<Component
renderItem={<SimpleCard props={...props}/>}
/>
是的,我相信这是可能的,就像你可以用react router
渲染组件来传递这样的道具一样
<Route path="/somepath/" component={MyComponent} />
<Route path="/someotherpath/" render={() => <MyOtherComponent props={"coolprop"}
<Component
renderItem={<SimpleCard props={...props}/>}
/>
是的,如果使用了多个道具{…props},则可以根据需要在组件的道具中传递
<Component {...props} renderItem={other component}/>
是的,如果使用了多个道具{…道具},则可以根据需要在组件的道具中传递
<Component {...props} renderItem={other component}/>
这似乎工作正常。您可能还想发布SimpleCard
的代码。我暂时使用了一个占位符。您如何调用renderList
?它突然开始工作。。。谢谢大家的帮助,我不知道为什么它以前不起作用。这似乎很好用。您可能还想发布SimpleCard
的代码。我暂时使用了一个占位符。您如何调用renderList
?它突然开始工作。。。谢谢大家的帮助,我不知道为什么它以前不起作用。