赞
踩
import React, { Component } from "react";
export default class App extends Component {
state = {
fontSize: 30,
};
render() {
const { fontSize } = this.state;
return (
<div>
<div style={{ color: "red", fontSize: fontSize + "px" }}>我是app</div>
</div>
);
}
}
.nav-bar { display: flex; height: 44px; line-height: 44px; text-align: center; } .nav-bar .left, .nav-bar .right { width: 80px; background: red; } .nav-bar .center { flex: 1; background: blue; }
import "./navbar.scss"
yarn add styled-components
vscode-styled-components
import React, { Component } from "react"; import styled from 'styled-components'; const AppWrap = styled.div` color: blue; li { color: pink; font-size: 20px; } li:hover{ color: yellow; } `; export default class App extends Component { title = ["玄幻", "武侠", "校园"]; render() { const title = this.title; return ( <div> <AppWrap > <div>我是测试样式的</div> <ul> { title.map(item => { return <li> { item } </li> }) } </ul> </AppWrap> </div> ); } }
import React from 'react'; // 方式1 // import styledComponents from 'styled-components'; // const SonWrap = styledComponents.div` // color: #eee; // background: pink; // ` // 方式2 import SonWrap from './Son_css'; export default function Son(props) { return ( <SonWrap> son </SonWrap> ) }
import styledComponents from 'styled-components';
const SonWrap = styledComponents.div`
color: #eee;
background: pink;
`
export default SonWrap
import React, { Component } from "react"; import styled from 'styled-components'; // 定义一个组件 为当前组件添加css样式等 当前组件为 input组件 const XZLinput = styled.input` color: orange; &:focus{ color: red; } ` export default class App extends Component { render() { return ( <div> <XZLinput type="text" placeholder="请输入"></XZLinput> </div> ); } }
color: ${props => props.color};
import React, { Component } from "react"; import styled from "styled-components"; const TestWrap = styled.div` color: ${props => props.color}; font-weight: 900; `; render() { return ( <div> <TestWrap color="red">我是props</TestWrap> </div> ); } }
import React, { Component } from "react"; import styled from "styled-components"; // 定义一个组件 为当前组件添加css样式等 当前组件为 input组件 // attrs为当前input组件添加默认属性等! const XZLinput = styled.input.attrs({ placeholder: "请填写密码", paddingLeft: props => props.left || "5px" })` padding-left: ${props => props.paddingLeft}; color: orange; &:focus { color: red; } `; export default class App extends Component { render() { return ( <div> <XZLinput type="password" left="20px"></XZLinput> </div> ); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。