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

288
Views
Creating service in React Typescript using axios

There are two different ways to create a service and call from the required funtional component.

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

Why not just make it a function?

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

Putting the function in a class or an object seems to only complicate and obfuscate in this case. It looks very much like a C#-influenced style. Not terrible or wrong per se, but a bit verbose and complicated when all you need is a pure function.

Also, you should use let or const keyword instead of var because of differences in scope. You're not making any errors in the code above because var is function-scoped, but it's almost always better to use const (or let if you need to modify it).

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!