react 的样式写法
内联样式
const Home3 = function () {
return (
<div style = {{ color: 'red',fontSize: '50px',background: 'green' }}>
函数式组件
<div>
)
}
外部引入
import ' ./index.css ';
function Home () {
return ( <div className = "home-div">
这里是home </div>
) }
第三方库 classname或者classnames都可以,装那个用那个
yarn add classname -S
npm i classnames -S
import classname from ' classname ';
const add = () =>{
return (
<div
className = {
classname( {
index.css中定义的类名 : true,
index.css中定义的类名 : false
} )
}
>
使用classname中的类名来控制添加那个样式
</div>
)
}
组件样式,一个组件就是一个样式
yarn add styled-components -S
npm i styled-components -S
import styled from ' styled-components ';
import { Fragment } from ' react '
const P = styled.p`
color: red;
font-size: 50px;
`
const Title = styled.div`
color: green;
font-size: 50px;
`
const add = () {
return (
<Fragment>
<P>
这里跟你定义的类名一样,要首字母大写
</P>
<Title>
这里跟你定义的类名一样,要首字母大写
</Title>
</Fragment>
)
}