2017-12-23 1 views
1

私はHOCを使用しているコンポーネントを持っています。 HOCはthis.contextを必要としますが、何らかの理由でthis.contextが渡されていません。私は間違って何をしていますか? TYコンテキストが私のHOCに渡されない理由

コンポーネント:

import React, { Component } from 'react'; 
import withTracking from './hocs/withTracking'; 

class NamePage extends Component { 
    componentDidMount() { 
    console.log(this.context.mixpanel) // WORKS 
    } 
    render() { 
    return (
     ... 
    ); 
    } 
} 

NamePage.contextTypes = { 
    mixpanel: PropTypes.object.isRequired 
}; 

export default withTracking('View Page: NamePage')(NamePage); 

アドホック

import { lifecycle } from 'recompose'; 

export function withTracking(eventTitle) { 
    return lifecycle({ 
     componentDidMount() { 
      console.log(this.context.mixpanel) // UNDEFINED - fail-y? 
     } 
    }); 
} 

export default withTracking; 

にconsole.logが正しい返しundefinedを返している場合、コンポーネントのI出力。

私は間違っていますか?おかげ

答えて

1

ContextTypesは、あなたがwrappedComponentインスタンス上で、それを指定する必要がHOCでそれを使用するためには、唯一のNamePageコンポーネントに指定されている

const WrappedNamePage = withTracking('View Page: NamePage')(NamePage); 

WrappedNamePage.contextTypes = { 
    mixpanel: PropTypes.object.isRequired 
}; 

export default WrappedNamePage 
関連する問題