我的风格 从'react'导入React; 从'styled-components'导入样式;
export const DivMenuButton = styled.div`
border: 0px;
backgroundColor: #000;
height: 400px;
width: 200px;
`;
我的回报:
import { DivMenuButton } from './styles';
export default function Menu() {
const [open, setOpen] = useState(0);
const handleClick = e => {
e.preventDefault();
setOpen(!open);
};
return (
<DivMenuButton>
<Button
style={{ margin:0, padding: 0, height: "30px", width: "100%", borderRadius:'0px' }}
onClick={handleClick}
>
Button
</Button>
</DivMenuButton>
);
}
我也想知道我该怎么做:
我的状态为打开
我的div以400像素开头 点击该按钮将获得30px 但是我不知道如何使用样式化的组件来做到这一点
答案 0 :(得分:1)
使用样式组件props
尝试一下:
export const DivMenuButton = styled.div`
border: 0px;
background-color: #000; // was wrong syntax
height: 400px;
width: ${props => props.width}
`;
export default function Menu() {
const [open, setOpen] = useState(false);
const handleClick = e => {
// e.preventDefault(); no need
setOpen(!open);
};
return (
<DivMenuButton width={open ? '30px' : '400px'}>
<button
style={{ margin:0, padding: 0, height: "30px", width: "100%", borderRadius:'0px' }}
onClick={handleClick}
>
Button
</button>
</DivMenuButton>
);
}