The example you have given works only for function based components. How do I use it with class based components?