Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
Can i update Context API state in javascript class?

I have a Bluetooth class and listener method. I want to update my state in Bluetooth class and i will show in functional component.

JavaScript Class

import React, { Component } from "react";
import { MySampleContext } from "../../contexts/MySampleContext";

export class BluetoothClass extends Component {
    
    static contextType = MySampleContext;

    sampleBluetoothListener(value){
        this.context.updateMyState(value);
    }

}

This is my error.

TypeError: undefined is not an object (evaluating 'this.context.updateMyState')

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Yes you can, you can pass data down to your component tree using Context API.

// Context file 
import React from 'react';
const FormContext = React.createContext();
export default FormContext;
// Parent Class Component 
import FormContext from "../context";
class ParentClass extends Component {
  state = { name: "John Doe" };
  render() {
    return (
      <FormContext.Provider value={this.state.name}>
        <ChildClass />
      </FormContext.Provider>
    );
  }
}
// Child Class Component
import FormContext from "../context";
class ChildClass extends Component {
  render() {
    return (
      <FormContext.Consumer>
        {(context) => {
          console.log(context);
        }}
      </FormContext.Consumer>
    );
  }
}

The value prop in the context API takes an object, in which you could add a method that changes your state and pass it down to your component tree.

I advice you to take a quick look at the official Context API docs by React for a better understanding.

about 4 years ago · Juan Pablo Isaza Report

0

Thanks for all answers. But i mean pure "javascript class" not component, without rendering and react hooks is component based. Finally i solve problem with call back functions.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!