我们来观察一下刚写下的这几个组件,可以轻易地发现它们有两个重大的问题:

    • 有大量重复的逻辑:它们基本的逻辑都是,取出 context,取出里面的 store,然后用里面的状态设置自己的状态,这些代码逻辑其实都是相同的。
    • 对 context 依赖性过强:这些组件都要依赖 context 来取数据,使得这个组件复用性基本为零。想一下,如果别人需要用到里面的 ThemeSwitch 组件,但是他们的组件树并没有 context 也没有 store,他们没法用这个组件了。对于第一个问题,我们在 高阶组件 的章节说过,可以把一些可复用的逻辑放在高阶组件当中,高阶组件包装的新组件和原来组件之间通过 props 传递信息,减少代码的重复程度。

    对于第二个问题,我们得弄清楚一件事情,到底什么样的组件才叫复用性强的组件。如果一个组件对外界的依赖过于强,那么这个组件的移植性会很差,就像这些严重依赖 context 的组件一样。

    如果一个组件的渲染只依赖于外界传进去的 props 和自己的 state,而并不依赖于其他的外界的任何数据,也就是说像纯函数一样,给它什么,它就吐出(渲染)什么出来。这种组件的复用性是最强的,别人使用的时候根本不用担心任何事情,只要看看 PropTypes 它能接受什么参数,然后把参数传进去控制它就行了。

    我们把这种组件叫做 Pure Component,因为它就像纯函数一样,可预测性非常强,对参数(props)以外的数据零依赖,也不产生副作用。这种组件也叫 Dumb Component,因为它们呆呆的,让它干啥就干啥。写组件的时候尽量写 Dumb Component 会提高我们的组件的可复用性。

    到这里思路慢慢地变得清晰了,我们需要高阶组件帮助我们从 context 取数据,我们也需要写 Dumb 组件帮助我们提高组件的复用性。所以我们尽量多地写 Dumb 组件,然后用高阶组件把它们包装一层,高阶组件和 context 打交道,把里面数据取出来通过 props 传给 Dumb 组件。

    实例图片

    我们把这个高阶组件起名字叫 connect,因为它把 Dumb 组件和 context 连接(connect)起来了:

    1. import React, { Component } from 'react'
    2. import PropTypes from 'prop-types'
    3. export connect = (WrappedComponent) => {
    4. class Connect extends Component {
    5. static contextTypes = {
    6. store: PropTypes.object
    7. }
    8. // TODO: 如何从 store 取数据?
    9. render () {
    10. return <WrappedComponent />
    11. }
    12. }
    13. return Connect
    14. }

    connect 函数接受一个组件 WrappedComponent 作为参数,把这个组件包含在一个新的组件 Connect 里面,Connect 会去 context 里面取出 store。现在要把 store 里面的数据取出来通过 props 传给 WrappedComponent

    但是每个传进去的组件需要 store 里面的数据都不一样的,所以除了给高阶组件传入 Dumb 组件以外,还需要告诉高级组件我们需要什么数据,高阶组件才能正确地去取数据。为了解决这个问题,我们可以给高阶组件传入类似下面这样的函数:

    1. const mapStateToProps = (state) => {
    2. return {
    3. themeColor: state.themeColor,
    4. themeName: state.themeName,
    5. fullName: `${state.firstName} ${state.lastName}`
    6. ...
    7. }
    8. }

    这个函数会接受 store.getState() 的结果作为参数,然后返回一个对象,这个对象是根据 state 生成的。mapStateTopProps 相当于告知了 Connect 应该如何去 store 里面取数据,然后可以把这个函数的返回结果传给被包装的组件:

    1. import React, { Component } from 'react'
    2. import PropTypes from 'prop-types'
    3. export const connect = (mapStateToProps) => (WrappedComponent) => {
    4. class Connect extends Component {
    5. static contextTypes = {
    6. store: PropTypes.object
    7. }
    8. render () {
    9. const { store } = this.context
    10. let stateProps = mapStateToProps(store.getState())
    11. // {...stateProps} 意思是把这个对象里面的属性全部通过 `props` 方式传递进去
    12. return <WrappedComponent {...stateProps} />
    13. }
    14. }
    15. return Connect
    16. }

    connect 现在是接受一个参数 mapStateToProps,然后返回一个函数,这个返回的函数才是高阶组件。它会接受一个组件作为参数,然后用 Connect 把组件包装以后再返回。 connect 的用法是:

    1. ...
    2. const mapStateToProps = (state) => {
    3. return {
    4. themeColor: state.themeColor
    5. }
    6. }
    7. Header = connect(mapStateToProps)(Header)
    8. ...

    有些朋友可能会问为什么不直接 const connect = (mapStateToProps, WrappedComponent),而是要额外返回一个函数。这是因为 React-redux 就是这么设计的,而个人观点认为这是一个 React-redux 设计上的缺陷,这里有机会会在关于函数编程的章节再给大家科普,这里暂时不深究了。

    我们把上面 connect 的函数代码单独分离到一个模块当中,在 src/ 目录下新建一个 react-redux.js,把上面的 connect 函数的代码复制进去,然后就可以在 src/Header.js 里面使用了:

    1. import React, { Component } from 'react'
    2. import PropTypes from 'prop-types'
    3. import { connect } from './react-redux'
    4. class Header extends Component {
    5. static propTypes = {
    6. themeColor: PropTypes.string
    7. }
    8. render () {
    9. return (
    10. <h1 style={{ color: this.props.themeColor }}>React.js 小书</h1>
    11. )
    12. }
    13. }
    14. const mapStateToProps = (state) => {
    15. return {
    16. themeColor: state.themeColor
    17. }
    18. }
    19. Header = connect(mapStateToProps)(Header)
    20. export default Header

    可以看到 Header 删掉了大部分关于 context 的代码,它除了 props 什么也不依赖,它是一个 Pure Component,然后通过 connect 取得数据。我们不需要知道 connect 是怎么和 context 打交道的,只要传一个 mapStateToProps 告诉它应该怎么取数据就可以了。同样的方式修改 src/Content.js

    1. import React, { Component } from 'react'
    2. import PropTypes from 'prop-types'
    3. import ThemeSwitch from './ThemeSwitch'
    4. import { connect } from './react-redux'
    5. class Content extends Component {
    6. static propTypes = {
    7. themeColor: PropTypes.string
    8. }
    9. render () {
    10. return (
    11. <div>
    12. <p style={{ color: this.props.themeColor }}>React.js 小书内容</p>
    13. <ThemeSwitch />
    14. </div>
    15. )
    16. }
    17. }
    18. const mapStateToProps = (state) => {
    19. return {
    20. themeColor: state.themeColor
    21. }
    22. }
    23. Content = connect(mapStateToProps)(Content)
    24. export default Content

    connect 还没有监听数据变化然后重新渲染,所以现在点击按钮只有按钮会变颜色。我们给 connect 的高阶组件增加监听数据变化重新渲染的逻辑,稍微重构一下 connect

    1. export const connect = (mapStateToProps) => (WrappedComponent) => {
    2. class Connect extends Component {
    3. static contextTypes = {
    4. store: PropTypes.object
    5. }
    6. constructor () {
    7. super()
    8. this.state = { allProps: {} }
    9. }
    10. componentWillMount () {
    11. const { store } = this.context
    12. this._updateProps()
    13. store.subscribe(() => this._updateProps())
    14. }
    15. _updateProps () {
    16. const { store } = this.context
    17. let stateProps = mapStateToProps(store.getState(), this.props) // 额外传入 props,让获取数据更加灵活方便
    18. this.setState({
    19. allProps: { // 整合普通的 props 和从 state 生成的 props
    20. ...stateProps,
    21. ...this.props
    22. }
    23. })
    24. }
    25. render () {
    26. return <WrappedComponent {...this.state.allProps} />
    27. }
    28. }
    29. return Connect
    30. }

    我们在 Connect 组件的 constructor 里面初始化了 state.allProps,它是一个对象,用来保存需要传给被包装组件的所有的参数。生命周期 componentWillMount 会调用调用 _updateProps 进行初始化,然后通过 store.subscribe 监听数据变化重新调用 _updateProps

    为了让 connect 返回新组件和被包装的组件使用参数保持一致,我们会把所有传给 Connectprops 原封不动地传给 WrappedComponent。所以在 _updateProps 里面会把 statePropsthis.props 合并到 this.state.allProps 里面,再通过 render 方法把所有参数都传给 WrappedComponent

    mapStateToProps 也发生点变化,它现在可以接受两个参数了,我们会把传给 Connect 组件的 props 参数也传给它,那么它生成的对象配置性就更强了,我们可以根据 store 里面的 state 和外界传入的 props 生成我们想传给被包装组件的参数。

    现在已经很不错了,Header.jsContent.js 的代码都大大减少了,并且这两个组件 connect 之前都是 Dumb 组件。接下来会继续重构 ThemeSwitch


    因为第三方评论工具有问题,对本章节有任何疑问的朋友可以移步到 React.js 小书的论坛 发帖,我会回答大家的疑问。