Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda