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

502
Views
Qué establecer como valor del proveedor usando react typescript

Estoy tratando de aprender a usar ganchos y contexto en React, pero necesito trabajar con TypeScript (también nuevo en eso).

Estoy confundido acerca de qué establecer el valor del proveedor en mi código a continuación. Creé un gancho useFirebase que devolverá algunas funciones como RegisterUser, SignIn, user (es un estado), cerrar sesión.

Primero creé el gancho UseAuth.tsx, usando este gancho quiero importar las funciones anteriores que mencioné.

 import { useContext } from 'react'; import { AuthContext } from '../context/AuthProvider'; const UseAuth = () => { const auth = useContext(AuthContext) return auth }; export default UseAuth;

Todo está bien, pero recibo un error en el valor del proveedor. El error es (JSX attribute) React.ProviderProps<null>.value: null Type '{ RegisterUser: RegisterUserFunction; SignIn: SignInUserFunction; user: userState; logout: () => void; }' is not assignable to type 'null'. The expected type comes from property 'value' which is declared here on type 'IntrinsicAttributes & ProviderProps<null>' He creado una interfaz IAuth pero donde necesito definirla estoy confundido.

 import React, { createContext } from 'react'; import { UseFirebase } from '../hooks/UseFirebase'; export const AuthContext = createContext(null) export interface IAuth { RegisterUser: () => void; SignIn : () => void; user : any logout : () => void; } const AuthProvider = ({ children } :any) => { const allContext = UseFirebase() return ( <AuthContext.Provider value={allContext}> {children} </AuthContext.Provider> ); }; export default AuthProvider;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

el problema esta en

 export const AuthContext = createContext(null)

Debe pasarle un tipo adecuado, para que no se queje cuando intente asignar cualquier valor.

Prueba algo como

 React.createContext<IAuth>(initialValue);

Para obtener una explicación más detallada, consulte este artículo react-context-with-typescrip

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!