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

221
Vistas
Wrapping the app component with a context and getting the TypeError: (destructured parameter) is undefined

I'm trying to wrap all components under the app in a context to provide the things that I want

(as You can see it's my UserContext component)

enter code here
 
import React, { useState, createContext, useContext } from 'react'

const Context = createContext();
export let useUserContext = () => useContext(Context);

export default function UsersContext({ children }) {
  const [users, setUsers] = useState();

  const createUser = (user) => {
    if (!user.name || !user.email || !user.password) return;
    const newUsers = [...users, user];
    setUsers(newUsers);
  }

  return (
    <Context.Provider value={{ users, createUser }}>


      {children}
    </Context.Provider>
  )
}

(it is my app component)

enter code here
  import Nav from "./components/nav/Nav";
  import Container from "./components/container/Container";
  import { BrowserRouter } from "react-router-dom";

  import UsersContext from "./components/contexts/UserContext";


  function App() {

    return (
      <UsersContext>
        <BrowserRouter>
          <Nav />
          <Container />
        </BrowserRouter>
      </UsersContext>
    );
  }

  export default App;

It's used to be like this in my projects and I didn't have any problem but now the error I'm getting "TypeError: (destructured parameter) is undefined" also says that it's because of the children in UserContext In my opinion it shouldn't happen maybe you can help me to find the problem I can't see.

enter image description here

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Try: <Context.Provider value={[ users, { createUser } ]}>

instead of: <Context.Provider value={{ users, createUser }}>

edit:

also might try: instead of

    const newUsers = [...users, user];
    setUsers(newUsers);

do

    setUsers((currentUsers) => [...currentUsers, user]);
over 4 years ago · Santiago Trujillo Denunciar

0

I found the problem. it was because of the useState. it was undefined and I was calling the property that related to useState at the UserContext.

over 4 years ago · Santiago Trujillo 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