账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    react高阶组件HOC里的实例方法能在被包裹的组件中调用吗?
    58
    0

    使用高阶组件HOC抽离了一些重复的逻辑代码,可是结果是我在组件当中点击按钮去调用抽离出去的那个方法,是报错的,我记得mixin混入可以这么干吧,都调不了HOC里面的方法, 抽离出去还有啥意义啊

    HOC的代码是这样的

    import React from 'react'
    
    export default function withList(WrappedComponent, selectData) {
        return class extends React.Component {
            constructor(props) {
                super(props)
                this.pageSize = 10
                this.getSource = this.getSource.bind(this)
            }
            componentDidMount() {
                this.onSearch()
            }
            // 检索列表
            onSearch() {
                let searchData = this.props.form.getFieldsValue()  // 搜索框为空的时候表单 name: undefined
                this.setState({ currentPage: 1, searchData }, () => {
                    this.getSource()
                })
            }
            // 初始化数据
            getSource() {
                const formData = this.state.searchData
                const data = {
                    pageSize: this.pageSize,
                    pageNumber: this.state.currentPage,
                    ...formData         // 搜索框为空的时候name: undefined
                }
                this.setState({ loading: true })
                var a = Service.list(data);
                console.log(typeof a);
                a.then(res => {
                    let { status, msg, data } = res
                    this.setState({
                        list: data ? data.list : [],
                        total: data ? data.total : 0,
                        loading: false
                    })
                }).catch(err => {
                    this.setState({ loading: false })
                    if (err.status == 6003) {
                        this.setState({ list: [], total: 0 })
                    } else {
                        message.error(err.msg)
                    }
                })
            }
            render() {
                return (
                    <WrappedComponent data={this.state} {...this.props}/>
                )
            }
        }
    }

    我在组件中去调用这些方法是不可行的,

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 紫光极夜 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部