reactjs / react.dev

The React documentation website
https://react.dev/
Creative Commons Attribution 4.0 International
10.88k stars 7.46k forks source link

Object oriented programming Over functional programming? #3982

Open more03625 opened 2 years ago

more03625 commented 2 years ago

I am Working on Admin Panel which has 30-40 Master table which contains CRUD functionality. There are few masters which I need to call again and again in different masters. Eg we need to call country master in State & City inorder to add State and City because they both should in particular country. So the problem is I have to write or copy paste entire getCountry() Code in State & again in City. In OOPs we used to just create function at once then call it anywhere with single line of code.

But react that's not working as expected. So what's the best way to do that? Hope I am clear here.

Points / My concern 1) There are lot of repetition for calling APIs. 2) In oops we create function and just call that using single like and use it. But not in react we have to write setState logic and need to handle success, error logic everywhere again and again

RookTKO commented 2 years ago

You do not each functional component to have stated. You can create reusable utilitarian functions besides function (or class) components as well as have functions in functional components in multiple different ways.

Maybe I don't fully understand the concern?

shrirambalaji commented 2 years ago

This is probably not specific to react really, and how you choose to handle business logic. Have a Table component that handles this logic based on the type of data it is receiving (country, state, city), and let that component handle error and success.

FWIW, I don't understand when you say "OOPS" vs "Functional". setState happens in a class based component, so it's technically OOPS by definition. If you're talking about useState that's functional.