经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » React » 查看文章
深入理解react 组件类型及使用场景
来源:jb51  时间:2019/3/8 8:39:31  对本文有异议

函数组件 vs 类组件

函数组件(Functional Component )和类组件(Class Component),划分依据是根据组件的定义方式。函数组件使用函数定义组件,类组件使用ES6 class定义组件

  1. // 函数组件
  2. function Welcome(props) {
  3. return <h1>Hello, {props.name}</h1>;
  4. }
  5.  
  6. // 类组件
  7. class Welcome extends React.Component {
  8. render() {
  9. return <h1>Hello, {this.props.name}</h1>;
  10. }
  11. }
  12.  
  1. 函数组件的写法要比类组件简洁,不过类组件比函数组件功能更加强大。函数组件更加专注和单一,承担的职责也更加清晰,它只是一个返回React 元素的函数,只关注对应UI的展现。函数组件接收外部传入的props,返回对应UI的DOM描述,
  2. 类组件可以维护自身的状态变量,即组件的state,类组件还有不同的生命周期方法,可以让开发者能够在组件的不同阶段(挂载、更新、卸载),对组件做更多的控制。

无状态组件 vs 有状态组件

函数组件一定属于无状态组件 (划分依据是根据组件内部是否维护state)

  1. // 无状态组件
  2. class Welcome extends React.Component {
  3. render() {
  4. return <h1>Hello, {this.props.name}</h1>;
  5. }
  6. }
  7.  
  8.  
  9. // 有状态类组件
  10. class Welcome extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. show: true
  15. }
  16. }
  17. render() {
  18. const { show } = this.state;
  19. return (
  20. <>
  21. { show && <h1>Hello, {this.props.name}</h1> }
  22. </>
  23. )
  24. }
  25. }

展示型组件 vs 容器型组件

展示型组件(Presentational Component)和容器型组件(Container Component),划分依据是根据组件的职责。 (展示型组件一般是无状态组件,不需要state)

  1. class UserListContainer extends React.Component{
  2. constructor(props){
  3. super(props);
  4. this.state = {
  5. users: []
  6. }
  7. }
  8.  
  9. componentDidMount() {
  10. var that = this;
  11. fetch('/path/to/user-api').then(function(response) {
  12. response.json().then(function(data) {
  13. that.setState({users: data})
  14. });
  15. });
  16. }
  17.  
  18. render() {
  19. return (
  20. <UserList users={this.state.users} />
  21. )
  22. }
  23. }
  24.  
  25. function UserList(props) {
  26. return (
  27. <div>
  28. <ul className="user-list">
  29. {props.users.map(function(user) {
  30. return (
  31. <li key={user.id}>
  32. <span>{user.name}</span>
  33. </li>
  34. );
  35. })}
  36. </ul>
  37. </div>
  38. )
  39. }

展示型组件和容器型组件是可以互相嵌套的,展示型组件的子组件既可以包含展示型组件,也可以包含容器型组件,容器型组件也是如此。例如,当一个容器型组件承担的数据管理工作过于复杂时,可以在它的子组件中定义新的容器型组件,由新组件分担数据的管理。展示型组件和容器型组件的划分完全取决于组件所做的事情。

总结

通过上面的介绍,可以发现这三组概念有很多重叠部分。这三组概念都体现了关注点分离的思想:UI展现和数据逻辑的分离。函数组件、无状态组件和展示型组件主要关注UI展现,类组件、有状态组件和容器型组件主要关注数据逻辑。但由于它们的划分依据不同,它们并非完全等价的概念。它们之间的关联关系可以归纳为:函数组件一定是无状态组件,展示型组件一般是无状态组件;类组件既可以是有状态组件,又可以是无状态组件,容器型组件一般是有状态组件。

举个🌰

  1. import React, { Component } from 'react'
  2. import { observer } from 'mobx-react'
  3. import { Switch } from 'antd'
  4.  
  5. @observer
  6. class BaseInfoView extends Component {
  7. constructor(props) {
  8. super(props)
  9. }
  10.  
  11. render() {
  12. const {
  13. ideaName,
  14. resourceLocationDto,
  15. switchState,
  16. slotId
  17. } = this.props.model
  18.  
  19. return (
  20. <div>
  21. <div className="adx-form-item-title">基本信息</div>
  22. <div className="ant-form-horizontal">
  23. <div className="ant-form-item region">
  24. <label className="ant-col-4 ant-form-item-label">
  25. <span className="require-tip">*</span>创意名称:
  26. </label>
  27. <div className="ant-col-19 ml10 region-input">
  28. <div className="ant-form-text">
  29. { ideaName }
  30. </div>
  31. </div>
  32. </div>
  33.  
  34. <div className="ant-form-item region">
  35. <label className="ant-col-4 ant-form-item-label">
  36. <span className="require-tip">*</span>所属资源位:
  37. </label>
  38. <div className="ant-col-19 ml10 region-input">
  39. <div className="ant-form-text">
  40. { resourceLocationDto && resourceLocationDto.resourceName }
  41. </div>
  42. </div>
  43. </div>
  44.  
  45. <div className="ant-form-item region">
  46. <label className="ant-col-4 ant-form-item-label">
  47. <span className="require-tip">*</span>创意状态:
  48. </label>
  49. <div className="ant-col-19 ml10 region-input">
  50. <div className="ant-form-text">
  51. <Switch checked={switchState == 1}/>
  52. </div>
  53. </div>
  54. </div>
  55.  
  56. <div className="ant-form-item region">
  57. <label className="ant-col-4 ant-form-item-label">
  58. <span className="require-tip">*</span>推啊广告位ID
  59. </label>
  60. <div className="ant-col-19 ml10 region-input">
  61. <div className="ant-form-text">
  62. { slotId }
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. </div>
  68. )
  69. }
  70. }
  71.  
  72. export default BaseInfoView
  73.  

完全可以写成函数组件

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号