Skip to content

Context Api pass as props return undefined #19704

Description

@alz10

Environment

OS: Windows 10
Node: 8.11.1
Yarn: Not Found
npm: 6.1.0
Watchman: Not Found
Xcode: N/A
Android Studio: Version  3.1.0.0 AI-173.4720617

Packages: (wanted => installed)
react: 16.3.1 => 16.3.1
react-native: 0.55.4 => 0.55.4

Description

I'm using context api and wanted to access context outside the render() function for example in a life cycle method componentDidMount().

Steps to Reproduce

I tried to use this tutorial https://reactjs.org/docs/context.html#accessing-context-in-lifecycle-methods. I passed down the context to a props but it gave undefined when i console log this.props.context

Expected Behavior

Access context outside render()

Actual Behavior

const contextToProps = () => (
  <AppConsumer>{context => <TestComponent context={context} />}</AppConsumer>
);

class TestComponent extends PureComponent {
  componentDidMount() {
    console.log(this.props.context); //Underfined
  }
     render() {
       return()
   }
}

Am i doing it correctly?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    StaleThere has been a lack of activity on this issue and it may be closed soon.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions