5

I'm trying to get into react for side projects but my java and backend background in general really make things tough. Let's say I have a few data manipulation functions that I want to extract to a separate service and inject it using react hooks (since that's what everyone is using nowadays apparently). I can see it being much more elegant than props, but all the examples I can find resolve around passing state here and there, not passing actual dependencies like a stateless service. Any ideas how I should solve this?

Comments
  • 3
    Can't help with that, but welcome to devRant! Have your first ++.
  • 1
    @iAmNaN thanks, have been lurking for a while ;)
  • 2
    I wish I had a good example, I have a small hooks project but its currently not public.

    And its not a very good example on splitting things up yet as I am learning my self.

    One major difference is how state is stored, instead of maintaining a state object and having to use setState with sometimes very messy syntax every separate root object is created with useState which returns the current value an a set method specific to that root object.

    The important part is that all useState must always be in the same order and you should avoid placing any of them in an if since the order is paramount.

    Once you have the value and set method you are free to pass those around even to child objects that can then use the set method to update the state easily.

    Also all lifetime management methods have been replaced with useEffect and a few other use methods.

    Takes some time to get used to but the code is very compact and elegant once you get the gist of it :)
  • 2
    If you’re used to static typing then try TypeScript
  • 1
    @devTea im leaning more towards Flow. I've used TS in angular before, but it feels too much like Java. I prefer more flexible languages like Scala, so React seems like a better choice
  • 1
    @Voxera Thanks for the extensive reply, but what you've described is exactly what I have a problem with. You're describing managing *state*, while I'm referring to *stateless* services. Basically for now I decided to just build these services as objects with exported functions and import them where I need am. Works fine as long they don't depend on one another. I guess I'll figure out a nice, clean way eventually
  • 0
    @humblehound well, react hooks is just a function component with a few extra use... functions to add the same functionality as ordinary class components.

    They can do no more and no less just in another way.

    So if you do not need state just send the props as arguments and return the content to be displayed.
Add Comment