我使用風格的組件樣式組件中的父和子元素:風格元件 - 爲什麼道具位置會影響造型?
function StyledDemo({
name,
light,
...props
}) {
return (
<Parent {...props}>
<Child>{name}</Child>
</Parent>
);
}
我有一個light
道具是真/假 - 但我有與造型基礎元素的問題在該屬性的值:
const Parent = styled.div`
background-color: #000;
width: 100%;
${props => props.light && `
background-color: #ccc;
`}
`;
的造型似乎只能當我刪除的道具被傳遞到功能單獨工作。
Parent
元素使用基於當light
支柱值正確造型:
function StyledDemo({ name, ...props })
Parent
元素不使用基於light
道具價值正確的造型時:
function StyledDemo({ name, light, ...props })
我可以得到它的所有工作通過設置Parent
和Child
組件上的支柱,但是這似乎並不像它的最好辦法:
return (
<Parent {...props} light={light}>
<Child light={light}>{name}</Child>
</Parent>
);
這是應用基於道具樣式組件的正確方法,還是有我的方法的問題嗎?
我有一個演示用,如果它有助於修補: https://www.webpackbin.com/bins/-Kfcsujw99cjU7ttqgTz