Wang's blog Wang's blog
首页
  • 前端文章

    • HTML教程
    • CSS
    • JavaScript
  • 前端框架

    • Vue
    • React
    • VuePress
    • Electron
  • 后端技术

    • Npm
    • Node
    • TypeScript
  • 编程规范

    • 规范
  • 我的笔记
  • Git
  • GitHub
  • VSCode
  • Mac工具
  • 数据库
  • Google
  • 服务器
  • Python爬虫
  • 前端教程
更多
收藏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Wang Mings

跟随大神,成为大神!
首页
  • 前端文章

    • HTML教程
    • CSS
    • JavaScript
  • 前端框架

    • Vue
    • React
    • VuePress
    • Electron
  • 后端技术

    • Npm
    • Node
    • TypeScript
  • 编程规范

    • 规范
  • 我的笔记
  • Git
  • GitHub
  • VSCode
  • Mac工具
  • 数据库
  • Google
  • 服务器
  • Python爬虫
  • 前端教程
更多
收藏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Python爬虫

  • 前端教程

    • 团队规范

    • Project

    • JS

    • NodeJS

    • Vue

    • React

      • Vuex与Redux比较
      • 浅谈函数式编程
      • React 知识图谱
      • React源码分析 - 挂载和渲染
      • React源码分析 - 四大组件
      • React setState是异步吗
      • React 组件最佳实践
        • [#](#class组件) class组件
        • [#](#函数组件) 函数组件
      • ESlint代码检查规范 - React ReactNative
      • ReactNative Mac开发环境搭建
      • ReactNative Mac调试技巧
      • Redux
      • redux-actions
      • redux-sage
    • 效率工具

    • 读书笔记

  • 教程
  • 前端教程
  • React
wangmings
2022-07-19
目录

React 组件最佳实践

# # React 组件最佳实践

# # class组件

    import React, { Component } from 'react'
    import { observer } from 'mobx-react'
    import { string, object } from 'prop-types'
    // 分开本地导入和依赖导入
    import ExpandableForm from './ExpandableForm'
    import './styles/ProfileContainer.css'
    
    // 使用修饰器(如果有的话)
    @observer
    export default class ProfileContainer extends Component {
      state = { expanded: false }
      // 初始化state (ES7) 或者在构造函数(constructor)中初始化state (ES6)
     
      //使用静态属性(ES7)声明propTypes越早越好
      static propTypes = {
        model: object.isRequired,
        title: string
      }
    
      // 在propTypes后声明defaultProps
      static defaultProps = {
        model: {
          id: 0
        },
        title: 'Your Name'
      }
    
      // 使用箭头函数绑定指向定义的上下文的this
      handleSubmit = (e) => {
        e.preventDefault()
        this.props.model.save()
      }
      
      handleNameChange = (e) => {
        this.props.model.name = e.target.value
      }
      
      handleExpand = (e) => {
        e.preventDefault()
        this.setState(prevState => ({ expanded: !prevState.expanded }))
      }
      
      render() {
        // 解构props成可读的
        const {
          model,
          title
        } = this.props
        return ( 
          <ExpandableForm 
            onSubmit={this.handleSubmit} 
            expanded={this.state.expanded} 
            onExpand={this.handleExpand}>
            // 如果有2个以上props,分行写
            <div>
              <h1>{title}</h1>
              <input
                type="text"
                value={model.name}
                // onChange={(e) => { model.name = e.target.value }}
                // 避免创造新的闭包,应该使用下面的方法。
                onChange={this.handleNameChange}
                placeholder="Your Name"/>
            </div>
          </ExpandableForm>
        )
      }
    }
    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

# # 函数组件

    import React from 'react'
    import { observer } from 'mobx-react'
    import { func, bool } from 'prop-types'
    // 分开本地导入和依赖导入
    import './styles/Form.css'
    
    // 在组件前声明propTypes
    ExpandableForm.propTypes = {
      onSubmit: func.isRequired,
      expanded: bool,
      onExpand: func.isRequired
    }
    
    // 推荐使用具名组件,箭头函数定义的函数组件其实是没有命名的
    // 解构props,通过函数入参默认值的方式设定defaultProps
    function ExpandableForm({ onExpand, expanded = false, children, onSubmit }) {
      const formStyle = expanded ? { height: 'auto' } : { height: 0 }
      return (
        <form style={formStyle} onSubmit={onSubmit}>
          {children}
          <button onClick={onExpand}>Expand</button>
        </form>
      )
    }
    
    // 包装函数代替修饰器
    export default observer(ExpandableForm)
    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
编辑 (opens new window)
React setState是异步吗
ESlint代码检查规范 - React ReactNative

← React setState是异步吗 ESlint代码检查规范 - React ReactNative→

最近更新
01
theme-vdoing-blog博客静态编译问题
09-16
02
搜索引擎
07-19
03
友情链接
07-19
更多文章>
Theme by Vdoing | Copyright © 2019-2022 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式