0

I'm using the new Context API supported 'drizzle-react' library in my dapp. Can anyone please clarify how to receive the context sent from DrizzleContext.Provider into a "class" component? Check out the example index.js file below. Imagine the < App /> component is a "class" component and not a "function component". For a "function" component, i guess it is pretty straightforward - use < DrizzleContext.Consumer >. But how to get the context (drizzle = {drizzle}) inside the class component ? I tried giving a constructor with context prop like this:

constructor(props,context) {...}

but this doesnt work. Any ideas ?

my index.js looks like this:

<!-- language: lang-jsx -->
    import React from 'react';
    import ReactDOM from 'react-dom';
    import { Drizzle, generateStore } from "drizzle";
    import { DrizzleContext } from "drizzle-react";
    import { LoadingContainer } from 'drizzle-react-components'
    import './index.css';
    import App from './App';
    //import * as serviceWorker from './serviceWorker';
    import drizzleOptions from './drizzleOptions';

    const drizzleStore = generateStore(drizzleOptions);
    const drizzle = new Drizzle(drizzleOptions, drizzleStore);

        ReactDOM.render(
            (<DrizzleContext.Provider drizzle={drizzle} >
                    <App />
            </DrizzleContext.Provider>),
            document.getElementById('root')
        );

1 Answer 1

0

turns out that one can use this statement to get the context in class components :

MyClassComponent.contextType = DrizzleContext.Consumer;

now if we use the following context in constructor :

constructor(props,context) {
super(props)
console.log(this.context);
}

this will output the context object { drizzle, drizzleState, initialized } to console. Even though I'm not sure if MyClassComponent.contextType is the right way to go by, since I believe it is part of the legacy Context API (correct me if I'm wrong).

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.