Gui*_*eRZ 5 javascript css frontend sass reactjs
我正在使用 React 和 SASS,我有一个立方体组件,我想用动态宽度渲染它。但是,我在 .scss 中按此宽度进行操作。有一个例子。
我的组件:
<Cube style={{ width: '100px' }} />
<Cube style={{ width: '70px' }} />
<Cube style={{ width: '20px' }} />
Run Code Online (Sandbox Code Playgroud)
我的风格.scss:
$cubeWidth: 150px;
$cubeHeight: $cubeWidth;
#cube {
width: $cubeWidth;
height: $cubeHeight;
}
// In this cube, I have -of course- 6 faces, and I operate in my style.scss:
.cubeFace1 {
transform: translateZ($cubeWidth / 2);
}
... etc
Run Code Online (Sandbox Code Playgroud)
如何使我的 $cubeWidth 等于我的动态宽度?此外,.scss 被加载到 index.js 中,如下所示:
// React index.js
import './css/style.scss';
render(<Router />, document.getElementById('root'));
Run Code Online (Sandbox Code Playgroud)
谢谢 !
不可能从 React props 传递到 sass 变量。但是,您可以使用样式组件:https://styled-components.com/docs/basics#installation
npm i styled-componentsconst Cube = styled.div`
width: props.cubeWidth;
height: props.cubeHeight;
`;
const CubeFaceOne = styled.span`
transform: translateZ(props.cubeWidth / 2);
`;
Run Code Online (Sandbox Code Playgroud)
使用这种方法,Cube 和 CubeFace 将需要分成两个单独的组件,并以这种形式呈现,
render() {
return (
<Cube>
<CubeFaceOne />
<CubeFace />
<CubeFace />
...
</Cube>
)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2722 次 |
| 最近记录: |