我正在使用 react.js v15.6.1
我有一个样式如下的 css 文件:
.well {
-webkit-box-shadow: 1px 3px 1px #9E9E9E;
-moz-box-shadow: 1px 3px 1px #9E9E9E;
box-shadow: 1px 3px 1px #9E9E9E;
}
Run Code Online (Sandbox Code Playgroud)
我试图在 react.js 中使用它,但没有像下面这样工作:
import React, { Component } from "react";
var Bootstrap = require('react-bootstrap')
export default class Title extends Component {
render() {
return (
<div style={styles.well}>
<div style={styles.header}>Business Background</div>
<hr/>
<p>
hello hello
</p>
</div>
);
}
}
const styles = {
well:{
webkitBoxShadow: "1px 3px 1px #9E9E9E",
mozBoxShadow: "1px 3px 1px #9E9E9E",
boxShadow: "1px 3px 1px #9E9E9E"
}
};
Run Code Online (Sandbox Code Playgroud)
连我都改成了
well:{
boxShadow: "1px 3px 1px #9E9E9E"
}
Run Code Online (Sandbox Code Playgroud)
如果你看上图,Hello 1 是从 react 生成的,Hello 2 是来自 css 文件
我不想使用 css-loader 或 styled-components 库,因为我现在想让事情保持简单。
小智 7
只需实现上面的样式返回方法。
import React, { Component } from "react";
var Bootstrap = require('react-bootstrap')
export default class Title extends Component {
render() {
var well={
boxShadow: "1px 3px 1px #9E9E9E"
}
var header={
color:"#000",
fontWeight:"550"
}
return (
<div style={well}>
<div style={header}>Business Background</div>
<hr/>
<p>hello hello</p>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
这里的问题不是 boxShadow 本身,而是文件中设置样式的位置。
我倾向于将我的样式放在:1. 在组件上方的常量中,2. 在 Component 类中的“getStyles”类型方法内,3. 在通过 classNames 访问的更传统的 scss 文件内,或者 4. 仅内联风格
选项1:
const GREY = "#9E9E9E";
const styles = {
header: {
// styles go here!
},
well: {
boxShadow: `1px 3px 1px ${GREY}`,
},
};
const Title = props => (
<div style={styles.well}>
<div style={styles.header}>Business Background</div>
<hr />
<p>hello hello</p>
</div>
);
Run Code Online (Sandbox Code Playgroud)
这是选项#2:
class Title extends Component {
getStyles = () => {
const GREY = "#9E9E9E";
return {
header: {
// styles go here!
},
well: {
boxShadow: `1px 3px 1px ${GREY}`,
},
};
};
render() {
const styles = this.getStyles();
return (
<div style={styles.well}>
<div style={styles.header}>Business Background</div>
<hr />
<p>hello hello</p>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
````
| 归档时间: |
|
| 查看次数: |
41752 次 |
| 最近记录: |