[英]How do I pass all other attributes in props - ReactJS
const Label = (props) => {
return <label className={"card-label"} {...props.attributes}>{props.children}</label>;
};
If I try to access the attributes in other functions.如果我尝试访问其他函数中的属性。 I'm getting errors and unable to proceed
我收到错误,无法继续
<Label attributes={style:{margin:"10px"}}>Select Tip %</Label>
Does anyone know the answer?有人知道答案吗? How do I pass all other attributes of any component with props?
如何使用道具传递任何组件的所有其他属性?
You are destructuring attributes values directly to the component, so each attributes key is going to be considered as a prop.您正在将属性值直接解构到组件,因此每个属性键都将被视为一个道具。 On the second code, you are trying to access the attributes prop which won't be existed.
在第二个代码中,您试图访问不存在的属性道具。
You should put your attributes value into attributes prop:您应该将您的属性值放入属性道具:
return <label className={"card-label"} attributes={props.attributes}>{props.children}</label>;
A more readable way is to extract attributes from props directly:一种更易读的方法是直接从 props 中提取属性:
const Label = ({ attributes, children }) => {
return <label className={"card-label"} attributes={attributes}>{children}</label>;
};
I guess you have a issue with the syntax of passing props to Label component.Try with this我猜你对将道具传递给 Label 组件的语法有疑问。试试这个
const Label = (props) => {
return <label className={"card-label"} {...props.attributes}>{props.children}</label>;
};
return <Label attributes={{style:{margin:"50px"}}}>Select Tip %</Label>;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.