score:2

Accepted answer

resolved the issue with the help from @gemhar

changes to be made in authform.js

...
import authcontext from '../components/context/authcontext'

class authform extends react.component {

  //add this line
  static contexttype = authcontext;

  constructor(props) {
    super(props);
    this.state = {
      usernameinput: '',
      emailinput: '',
      passwordinput: '',
      confirmpasswordinput: '',
      remeberme: false,
      agreeterms: false,
      todashboard: false
    };
  }

  componentdidmount() {
    if (localstorage.token) {
      this.setstate(() => (
        {
          todashboard: true
        }
      ))
    }
  }
  componentdidupdate() {
      //i can access setauth here
      this.context.setauth(true)

      //or by destructuring
      let {setauth} = this.context;
      setauth(true)
  }

  render() {
    return (
        <div>
            //some code
        </div>
    );
  }
}

export default authform;

score:1

the most common way to access context from a class component is via the static contexttype. if you need the value from context outside of render, or in a lifecycle method, you'll use it this way.

import react from "react";
import authcontext from "./context";

class authform extends react.component {
  constructor(props) {
      ...
  }
  static contexttype = authcontext

  componentdidupdate() {
    const {setauth} = this.context
    // access the 'setauth' function here
  }

  render() {
    return <div>some code</div>;
  }
}

export default authform;

Related Query

More Query from same tag