Why is Context not being passed to my HOC

AnnaSm

I have a component that is using a HOC. The HOC needs this.context but for some reason this.context is not being passed. What am I doing wrong? ty

component:

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);

hoc

import { lifecycle } from 'recompose';

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

export default withTracking;

The console.log is returning undefined where it returns correct if I output in the component.

What am I doing wrong? Thanks

Shubham Khatri

ContextTypes are specified only for the NamePage component, in order for you to use it with the HOC you need to specify it on the wrappedComponent instance

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

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

export default WrappedNamePage

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why are my meteor settings not being passed to the application?

Why is my array being passed with an incorrect size?

Why is my input not being passed into the if statements?

Why is my search function not being passed?

Why is nothing being passed to my overloaded function?

Why is my DB context being disposed

Why are my props not being passed to my child component?

Why is only half my data being passed into my dictionary?

Why is my variable not properly being passed in React Native?

Why isn't my prop logging or being passed in React Native?

Why are my hidden input not being passed in the model class?

Why is my INT variable being passed by reference?? C#

Why aren't my arguments being passed to the parameters?

Why aren't the values from my directive being passed into the function?

Flutter: Why is my function passed to `StatelessWidget` not is not being executed?

Flutter: Why is my custom function passed to a `StatelessWidget` object not being executed?

Why is a function being passed in?

Why is the context not passed to eventlistener

Actions on Google context not being passed to next intent

Why does my operator + overload call my copy constructor despite being passed by reference?

Why is context passed to the constructor of Intent?

React.js Virgin: Why is my hard coded data being passed differently than data entered by user?

Why is a bad pointer being passed into my native exported methods from C#?

Why don't I have to specify that the result of a fortran function is being passed by value to my C++ program?

Parameters of my function in React child component are not being passed to the parent component's function. Why is this happening?

My onChange function is not being passed down to child

What is the hidden argument being passed to my `MethodType`?

why my HOC Component is working properly ? #React

Why it is required to return *this when this is still being passed?