21

Is there any way to get and handle a context value in another place except render method like constructor or a custom function that I declared in my component class?

In a way that I know, the consumer tag surrounds the fetch function but, I don't want to handle value in consumer tag.

Any idea?

2
  • 3
    I don't think that this is a duplicate, the linked question asks how to get context in lifecycle callbacks, while this one asks about getting it in the constructor. The approach proposed in the accepted answer doesn't work for constructor. Commented Aug 14, 2019 at 15:35
  • 12
    yes you can use ``` constructor(props, context){ super(props, context) // this.context } Commented Aug 20, 2019 at 8:09

2 Answers 2

22

This is possible, You may use like

constructor(props, context) {
    super(props, context)
 }
Sign up to request clarification or add additional context in comments.

4 Comments

This form is deprecated
@gman So what are you supposed to do instead?
The new context API is given here: reactjs.org/docs/context.html
how I can access context in the functional component
3
constructor(props, context) {
super(props);
// do what you want with context
console.log(context);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.