Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【feature】建议支持高阶组件 #1155

Closed
youngluo opened this issue Nov 16, 2018 · 15 comments
Closed

【feature】建议支持高阶组件 #1155

youngluo opened this issue Nov 16, 2018 · 15 comments
Assignees
Labels
enhancement New feature or request

Comments

@youngluo
Copy link

是否能够支持高阶组件

@yuche
Copy link
Contributor

yuche commented Nov 16, 2018

目前 Taro 的高阶组件只能劫持非 JSX 方法。有 JSX 的方法实际上是小程序模板,小程序的模板是没有继承这个说法的。

你可以参考 taro-redux 的实现
https://github.com/NervJS/taro/tree/master/packages/taro-redux
或者 taro-apollo
https://github.com/kdong007/taro-apollo

@taro-bot
Copy link

taro-bot bot commented Dec 19, 2018

CC @yuche

@yuche yuche added the enhancement New feature or request label Jan 9, 2019
@JohannLai
Copy link
Contributor

@yuche 请问目前的支持情况有变化吗?

@yuche
Copy link
Contributor

yuche commented Mar 5, 2019

已经有分支在开搞了

@youngluo
Copy link
Author

youngluo commented Mar 5, 2019

👏

@luoboding
Copy link

高阶组件都不能玩,感觉一下子失去了好多自由!

@youngluo
Copy link
Author

已经有分支在开搞了

@yuche 不知现在什么情况了

@yuche
Copy link
Contributor

yuche commented Jun 13, 2019

高阶组件规划在下一个 minor 版本

@Janice1114
Copy link

这个目前是有相关开发的分支吗?

@jackie-gan
Copy link

请问一下下一个minor版本预计是什么时候呢?同求分支

@leoswing
Copy link

高阶组件这块在小程序支持劫持render的有相关update么? @yuche 谢谢

@youngluo
Copy link
Author

请问一下下一个minor版本预计是什么时候呢?同求分支

同求

@bobsam
Copy link

bobsam commented Nov 18, 2019

请问高阶组件的方式什么时候可以呢?

@shenghanqin
Copy link
Collaborator

我是这么用的。嘻嘻

import { connect } from '@tarojs/redux'
import { modelName as userModel, getPassSdk } from '@/store/user'
import { LOGIN_STATUS } from 'constants/status'
import EventEmitter from 'utils/event-emitter'

/**
 * 为 page 级别组件添加公共方法,向被装饰组件塞入 props
 * @param {ReactComponentLike} TaroComponent
 */
export default function WithPage(TaroComponent) {
  @connect(state => ({
    loginStatus: state[userModel].loginStatus,
    isLogin: state[userModel].loginStatus === LOGIN_STATUS.SUCCESS,
    userInfo: state[userModel].userInfo
  }))
  class CorePage extends TaroComponent {
    static options = {
      addGlobalClass: true
    }

    componentWillMount() {

      // 全局通知登录状态
      if (typeof this.onInitLoginCallback === 'function') {
        EventEmitter.on('login-callback-status', this.__loginCallback)
      }
      
      if (super.componentWillMount) {
        super.componentWillMount()
      }
    }

    componentDidShow() {
      // taro中的Page是基于Component来写的,所以在Bisdk中没法在Page上注入监听
      if (process.env.TARO_ENV === 'weapp') {
        wx.bisdk && wx.bisdk.onPageShow && wx.bisdk.onPageShow()
      }
      if (super.componentDidShow) {
        super.componentDidShow()
      }
    }

    componentWillUnmount() {

      // 全局通知登录状态
      if (typeof this.onInitLoginCallback === 'function') {
        EventEmitter.off('login-callback-status', this.__loginCallback)
      }

      if (super.componentWillUnmount) {
        super.componentWillUnmount()
      }
    }

    __loginCallback = (loginStatus) => {  // 防止登录过程超过1s时回调
      this.onInitLoginCallback({
        ...this.props.userInfo,
        sourceType: 'async',
        loginStatus
      })
    }


    // TODO 这个方法可以等待废弃
    /**
     * 登录态监听
     */
    __checkLogin = () => {
      if (typeof this.onInitLoginCallback === 'function') {
        const { isLogin, userInfo } = this.props
        const passSdk = getPassSdk() || {}
        if (isLogin) {
          this.onInitLoginCallback({ sourceType: 'sync', ...userInfo })
        } else if (passSdk.initialized) {
          this.onInitLoginCallback({ sourceType: 'autoLoginFail', ...userInfo })
        }
      }
    }

  }

  CorePage.displayName = TaroComponent.displayName
    || TaroComponent.name
    || 'CorePage'

  return CorePage
}

@taro-bot
Copy link

taro-bot bot commented Dec 8, 2019

Hello~

您的问题楼上已经有了确切的回答,如果没有更多的问题这个 issue 将在 15 天后被自动关闭。

如果您在这 15 天中更新更多信息自动关闭的流程会自动取消,如有其他问题也可以发起新的 Issue。

Good luck and happy coding~

@taro-bot taro-bot bot removed the to be closed label Dec 23, 2019
@taro-bot taro-bot bot closed this as completed Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

9 participants