16

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?

Joe
  • 40,031
  • 18
  • 107
  • 123
Behnam Azimi
  • 1,944
  • 2
  • 29
  • 45
  • 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. – johnny Aug 14 '19 at 15:35
  • 11
    yes you can use ``` constructor(props, context){ super(props, context) // this.context } – Adam Aug 20 '19 at 08:09

2 Answers2

8

This is possible, You may use like

constructor(props, context) {
    super(props, context)
 }
Avi
  • 101
  • 1
  • 6
2
constructor(props, context) {
super(props);
// do what you want with context
console.log(context);
}
D. Krasnov
  • 21
  • 2