react做h5 例子_React 仿简书项目实战

项目源码

github.com/astak16/act…

项目结构

src/common; // 公共组件

src/pages; // 页面

src/static; // 静态资源

src/store; // 主 store

App.js; // 根组件

index.css; // 样式

index.js; // 入口文件

重置 css

搜索reset.css,复制过来

styled-components

css文件一旦在一个文件中被引入,会在全局中生效

这样写css会有些问题,当页面中有多个组件时,样式存在被覆盖的风险

我们希望在写样式的时候,每个组件的样式是独立的,不会互相的影响

使用

将index.css重命名为style.js

在index.js引入style.js

import "style.js";

style.js

import { injectGlobal } from "styled-components";

injectGlobal`

body{

padding: 10px

}

`;

在组件中使用

组件声明

// Header/style.js

import styled from "styled-components";

export const HeaderWrapper = styled.div`

height: 56px;

`;

// Header/index.js

import { HeaderWrapper } from "./style";

class Header extends Component{

render() {

return ;

}

}

图片使用

在style.js中使用背景图片的话,直接使用background: url('/images/logo.png')是没有效果的,因为webpack在打包的时候,不知道工程目录是啥样的,它会把路径当成字符串

需要这样写

import logoPic from "/images/logo.png";

export const Logo = style.a`

background: url(${logoPic})

`;

设置属性

import logoPic from "/images/logo.png";

export const Logo = style.a.attr({

href: "/",

})`

background: url(${logoPic})

`;

参数传递

组件中要传递不同的参数进来,styled提供了函数功能

1

1

//

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值