score:1

Accepted answer
import React from "react";
import ReactDOM from "react-dom";

import "./styles.css";

const Context = React.createContext({ message: "hi" });

class Child extends React.Component {
  constructor() {
    super();
    console.log("Constructor: ", this.context); // undefined here
  }
  render() {
    const message = this.context;
    return <p>{message}</p>; // hello here
  }
}

Child.contextType = Context;

function App() {
  return (
    <div className="App">
      <Context.Provider value="hello">
        <Child />
      </Context.Provider>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Trying to print the context in the constructor returns undefined, but you should be able to use context in the render function like this.

You can run the example here: https://codesandbox.io/s/crazy-forest-89bd6?fontsize=14

score:0

Try to rewrite your child component like one of them below:

Dynamic Context

import React from 'react';
import Context from './Context'; // Context here is the name you defined as Context = React.createContext();

class Join extends React.Component {
    render() {
        let props = this.props;
        let value = this.context;
        return (
            <div>
                {/* Something important here */}
            </div>
        );
    }
}

Join.contextType = Context; // Context here is the name you defined as Context = React.createContext();
export default Join;

Context.Consumer

import React from 'react';
import { Consumer } from './Context'; // the path is where your context file is

class Join extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <Consumer>
            {value => (
                {/* Here you can retrieve your messageThatllChange */}
                <div>
                    {/* Something important here */}
                </div>
            )
            </Consumer>
        );
    }
}

export default Join;

Related Query

More Query from same tag