19

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
  • 41,484
  • 20
  • 104
  • 125
Behnam Azimi
  • 2,260
  • 3
  • 34
  • 52
  • 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

18

This is possible, You may use like

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