classNames

官方地址
classname可以让你在react项目中像Vue一样添加class

  1. import React from "react";
  2. import classNames from 'classnames'
  3. class App extends React.Component {
  4. constructor(){
  5. super()
  6. this.state = {
  7. foo: true,
  8. isActive: true
  9. }
  10. }
  11. render(){
  12. const {foo, isActive} = this.state
  13. return (
  14. <div>
  15. {/* 常规 React 添加class方法 */}
  16. <h2 className={"hello " + (isActive ? "world" : "")}>标题1</h2>
  17. <h2 className="foo bar active title">标题1</h2>
  18. {/* 使用classnames */}
  19. <h2 className={classNames({hello: foo, active: isActive})}>标题1</h2>
  20. <h2 className={classNames('a', 'b', 'c')}>标题1</h2>
  21. <h2 className={classNames({hello: foo}, 'world')}>标题1</h2>
  22. </div>
  23. )}
  24. }
  25. export default App;

antd

安装 antd

  1. yarn add antd

安装 antd icon

  1. yarn add @ant-design/icons

在入口文件 index.js 中引入 antd 的样式文件

  1. import 'antd/dist/antd.css'

基本使用,查看文档即可

  1. import React from "react";
  2. import { Button, Space } from 'antd';
  3. import { PoweroffOutlined } from '@ant-design/icons'
  4. class App extends React.Component {
  5. constructor(){
  6. super()
  7. this.state = {
  8. loadings: true
  9. }
  10. }
  11. render(){
  12. const {loadings} = this.state
  13. return (
  14. <>
  15. <Space style={{ width: '100%' }}>
  16. <Button type="primary" loading>
  17. Loading
  18. </Button>
  19. <Button type="primary" size="small" loading>
  20. Loading
  21. </Button>
  22. <Button type="primary" icon={<PoweroffOutlined />} loading />
  23. </Space>
  24. </>
  25. )
  26. }

antd 默认是支持 tree-shaking 的,不需做额外配置

使用Craco修改默认配置

官方文档
我们可以通过 yarn eject 修改 webpack 配置来修改antd的配置,但这样会带来不确定的问题
因此推荐使用 craco 来修改配置

具体操作参照官方文档即可

路径别名

我们还可以使用 craco 设置路径别名

  1. const CracoLessPlugin = require('craco-less');
  2. const path = require('path');
  3. const resolve = dir => path.resolve(__dirname, dir);
  4. module.exports = {
  5. plugins: [
  6. // ...
  7. ],
  8. webpack: {
  9. alias: {
  10. "@": resolve("src"),
  11. "components": resolve("src/components")
  12. }
  13. }
  14. };

自定义样式

可以自己写CSS覆盖默认的样式

  1. import { Button, Input } from "antd"
  2. import styled from 'styled-components'
  3. const {TextArea} = Input
  4. const CutstomTextArea = styled(TextArea)`
  5. color: red;
  6. border: 3px dashed pink !important;
  7. `
  8. const CommentInput = () => {
  9. return (
  10. <div style={{padding:"0 20px"}}>
  11. <CutstomTextArea rows={4}/>
  12. <Button style={{
  13. background: 'pink',
  14. color: 'white',
  15. border: "1px solid pink",
  16. marginTop: 20,
  17. fontSize: 24,
  18. height: 60,
  19. width: 300,
  20. borderRadius: 30}}>发表评论</Button>
  21. </div>
  22. )
  23. }
  24. export default CommentInput

Dayjs的使用

dayjs是比moment更轻量级的库

  • 使用 fromNow() 计算相对时间
  • 使用 locale 设置显示的语言 ```javascript import {Comment, Tooltip, Avatar} from ‘antd’ import { DeleteOutlined } from ‘@ant-design/icons’ import * as dayjs from ‘dayjs’ import ‘dayjs/locale/zh-cn’ const relativeTime = require(‘dayjs/plugin/relativeTime’) dayjs.extend(relativeTime)

const CommentItem = (props) => { const { avatar, comment, user, createTime } = props.comment const actions = [ 删除 ] return (

{user}} avatar={} content={

{comment}

} datetime={ {dayjs(createTime).locale(‘zh-cn’).fromNow()} } />
) }

export default CommentItem ```