React nested setState (zod like) Library API and NPM publishing
Hi!. I'm looking for feedback on the API of this library (in image) and secondly the steps to publish it if is not a terrible idea.
I'm working on a multi-year warehouse management app and deal with deeply nested state daily so I made a helper function in the style of zod where everything is a function.
With this though the functions are built from existing state to help set state. So every nested key turns into a function to set the state at that point in the object.
Just want to get some feedback on the api that is in the image and if anyone can foresee any issues or has suggestions?
I've never made a library and only picked up react in December I'm a noob and would ultimately like to have a more experienced person get involved if anyone finds it interesting enough.
It uses a single useState to set the state and a recursive function updates the state at the right point and returns the whole object back to useState and updates. Runtime is as quick as a standard set state. TS performance is a bit laggy with autocomplete and type checking (you can see the type checking in the image).
As per the image comments, it deals with nested objects and arrays and has access to previous state and initial state in all function calls.
I think it's great for workflow but I'm sure i'm missing something obvious that you folk will pick up on.
I haven't made a github yet because that leads me on to the second question which is if I wanted to publish this what are the steps to take?
It currently resides inside my main next app. Do I create a barebones "npm install react react-dom" or a Vite installation for testing or what? Currently less than 300 lines for everything with no dependencies other than react.
If I get an outline of environment to set up for it, I'll set up a github so you can look at the code and test it yourself if you are inclined.
3 Replies
lots of bad typing in the image, my apologies.
Have you tried using Immer? It's not quite the API you have but it's great for doing nested/complex updates to immutable data: https://github.com/immerjs/immer
Personally I'm not a fan of the API you have there, but if it works for your use case then that's great!
Yeah I use immer almost everywhere. I'll be using this more going forward but if I need to filter the object or find an id or whatever before setting state I'll still use immer.
gonna change the api closing