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

287
Views
Creando servicio en React Typescript usando axios

Hay dos formas diferentes de crear un servicio y llamar desde el componente funcional requerido.

1.

 export const userProfileService = { ResetPassword: async (userId: string) => { var response = await http.get<string, boolean>(`UserProfile/ResetPassword?userId=${userId}`); return response; } }
 class UserProfileService { readonly userProfilePath = 'UserProfile'; resetPassword = async (userId: string) => { var response = await http.get<string, boolean> (`${this.userProfilePath}/ResetPassword?userId=${userId}`); return response; } export default new UserProfileService(); **The question is which way is better and optimized way? or better convention?**
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

¿Por qué no convertirlo simplemente en una función?

 export const resetPassword: async (userId: string) => { return await http.get<string, boolean>(`UserProfile/ResetPassword?userId=${userId}`); }

Poner la función en una clase o un objeto parece solo complicar y ofuscar en este caso. Se parece mucho a un estilo influenciado por C#. No es terrible o incorrecto per se, pero es un poco detallado y complicado cuando todo lo que necesita es una función pura.

Además, debe usar la palabra clave let o const en lugar de var debido a las diferencias en el alcance. No está cometiendo ningún error en el código anterior porque var tiene un alcance de función, pero casi siempre es mejor usar const (o let si necesita modificarlo).

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!