[英]Using props in React with styled components across components
I have a React component:我有一个反应组件:
const StyledButton = styled.button`
border: none;
background: ${props => props.color};
color: white;
&:focus {
outline: none;
}
`;
const Button = (props) => {
return (
<StyledButton>{props.name}</StyledButton>
)
};
and another component:和另一个组件:
const NoteWindow = (props) => {
return (
<StyledNoteWindow className="NoteWindow">
<StyledForm action="#" className="form">
<StyledInput type="text" name="title" id="title" autoComplete="off" placeholder="Title" style={{fontSize: 60, fontWeight: 700}}/>
<StyledInput type="text" name="content" id="content" autoComplete="off" placeholder="Content.." style={{fontSize: 20}}/>
</StyledForm>
<Button name="Discard" color="red"/>
<Button name="Save" color="blue"/>
</StyledNoteWindow>
)
}
and I want the background of my button component to be the "color" attribute in the other element.我希望我的按钮组件的背景是另一个元素中的“颜色”属性。 How can I do that?
我怎样才能做到这一点? (Everything is imported and exported)
(一切都是进口和出口)
From what I can see, Button
and StyledButton
components accepts at least the following props, name
and color
.据我所见,
Button
和StyledButton
组件至少接受以下道具、 name
和color
。
Therefore, you will need to pass the props down to the StyledButton
component:因此,您需要将道具向下传递给
StyledButton
组件:
const Button = ({ color, name }) => (
<StyledButton color={color}>{name}</StyledButton>
);
Destructure name
to used as child/text of button, and spread the rest of the passed props to StyledButton
.解构
name
以用作按钮的子/文本,并将传递的道具的 rest 传播到StyledButton
。
const Button = ({ name, ...props }) => {
return (
<StyledButton {...props}>{name}</StyledButton>
)
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.