DEV Community

Cover image for Javascript Proxy Magic: How I built a 2kB state manager with zero dependencies (and how it got me two different job offers)

Javascript Proxy Magic: How I built a 2kB state manager with zero dependencies (and how it got me two different job offers)

Lev Nahar on November 15, 2023

What is a state manager exactly? A state manager is a smart module that is capable of retaining session data (of an application or web application)...
Collapse
 
sultan99 profile image
Sultan • Edited

I did a similar library.
Image description

import {createState} from '@holycow/state'

// 👇 your store is a hook!
const useUser = createState({
  id: 1,
  name: 'Homer Simpson',
  address: {
    house: 742,
    street: 'Evergreen Terrace',
  },
})

const UserName = () => {
  const {name} = useUser() // 👈 value from the state
  return <div>{name}</div>
}

const {id, name, address} = useUser 
// any values 👆 from the hook can be used outside of components
Enter fullscreen mode Exit fullscreen mode
Collapse
 
lnahrf profile image
Lev Nahar

Looks great! Did you also use Proxies?

Collapse
 
sultan99 profile image
Sultan

Yes, I used proxy too.

Collapse
 
mfp22 profile image
Mike Pearson

MVC doesn't scale. Redux has boilerplate, but it scales.

Collapse
 
lnahrf profile image
Lev Nahar

I never said this should replace Redux :) I also don't know if this scales, but complexity wise Redux doesn't have to scale, it's extremely complicated from the get-go.
There is beauty in simplicity.

Collapse
 
mfp22 profile image
Mike Pearson

Come on. Complexity isn't binary. Let's say Redux is 3 out of 10 complex. This solution is 1 out of 10 complex for simple apps. Redux will grow to 5 out of 10 complex maximum, well this will grow to 10 out of 10 complex.

And when you say this doesn't replace Redux, do you mean they should use this inside a reducer?

Thread Thread
 
lnahrf profile image
Lev Nahar

Even if this could grow to a "10 out of 10" (whatever that means) complexity, you sure seem to have a very emotional reaction towards a state manager PoC that nobody uses. Have fun with that. Here is a quote from the readme:

"This project is not a replacement for other state management libraries, it's here to give a tiny, native solution for projects that wish to stay relatively small, and still enjoy state management".

Thread Thread
 
mfp22 profile image
Mike Pearson

Don't worry, people will use this. They thought Valtio was a good idea github.com/pmndrs/valtio

I'm not emotional, just direct. I have experience with dozens of state management libraries and I see what happens with each of them. People misuse Redux and it actually does turn into spaghetti code too. I'm about to write an article about that.

I just think it's a false trade-off between simplicity and scalability. I really don't like that the response to unidirectional data flow is to toss it out rather than improve upon the existing tools.

And it doesn't matter what your README says, if two things solve the same problem, even different sides of it, they're going to compete. People will use one over the other. They are substitutes. In a year I bet we see an article like "Why I chose VSSM over Redux" (or Jōtai or Zustand etc). Actually, that's kind of what this article is already.

Thread Thread
 
lnahrf profile image
Lev Nahar • Edited

If it doesn't matter what my readme says than it doesn't matter what I say.
Have fun with your experience with state management libraries, I will just keep doing me :)
P.S English is not my native language, not everybody is from the US.

Thread Thread
 
mfp22 profile image
Mike Pearson

I didn't notice any English mistakes.

Creating a state management library takes work and I respect that. It's a great learning exercise as well. But beyond that, you might be wasting your time, and I say that out of empathy. There are a lot of simple solutions out there already, and I would in particular recommend looking into Valtio and Jōtai. I created StateAdapt, but if bundle size matters most to you, don't use it - it's around 10kb. But if bundle size matters most to you, I'd recommend using SolidJS or Qwik, since both have built-in mechanisms somewhat similar to what you've created. I think Qwik even uses proxies too.

Thread Thread
 
dsaga profile image
Dusan Petkovic

We centrally don't need another state management library, but its good to explore different solutions and implementations, which Is what i believe this VSSM is, its perfectly fine and a great way to learn, and its not using anything other than what JS does natively.

Thread Thread
 
urielsouza29 profile image
Uriel dos Santos Souza

Image description

Thread Thread
 
mfp22 profile image
Mike Pearson

When you've never touched a complex application

Thread Thread
 
zryru profile image
Jorge Del Castillo

Sorry @mfp22 using your own phrase just being direct. You are wrong. Comparing this in terms of complexity with redux and stating that at most redux is a 5/10 in a complex application is just an incorrect fact. Redux is well known to be one of the least liked solutions due it's complexity. Even after you understand it and I personally like it and used to promote it before will always known that by many developers it is not a liked solution because of it's complexity.

Also know that you did not share your opinion alone it is clear it was an emotional response to protect redux which is completely out of the scope of the conversation in this post. I suggest next time don't overdue it and just share

Thread Thread
 
mfp22 profile image
Mike Pearson

Redux was mentioned in the 2nd paragraph. Redux has limitations. If you think Redux gets beyond 5/10, I look forward to you finding out what 6+ actually looks like, because it's only possible when event handlers have to do too much work. This is always happens when state update logic cannot be colocated with state declarations. It's the reason Flux was created. I recommend watching the original Flux + React talk a few times. Just because you haven't encountered the same problems as the Facebook team yet, doesn't mean you won't.

Thread Thread
 
mfp22 profile image
Mike Pearson

(Also I never use Redux anymore. It's just better than the knee-jerk reactions to it that never tried to understand what problems it was trying to solve.)

Thread Thread
 
lnahrf profile image
Lev Nahar

Mike what’s the point of arguing about such opinionated things? You’ve made your point, too bad it was not in a very nice way, but we will get over that. What is the purpose of this?

Thread Thread
 
mfp22 profile image
Mike Pearson

It must be nice not being opinionated like me. I can't help that when I see "Redux is too complicated" I get flashbacks from the battles I've fought in dozens of MVC codebases. I'm just sharing what I've learned. I would appreciate it if I were you. It's less nice to say nothing. I'm done. Good luck.

Thread Thread
 
lnahrf profile image
Lev Nahar

Alright mate

Collapse
 
ivan_jrmc profile image
Ivan Jeremic

Valtio over Redux for any app size I disagree on all points from @mfp22

Collapse
 
mfp22 profile image
Mike Pearson

Give me any complex app on GitHub using Valtio and I'll make it simpler. By complex I don't mean "big" like 30 pages - I mean pages with multiple kinds of events and effects, maybe heavily asynchronous. Maybe a real-time dashboard or something. I'll diagram the data flow and show that the number of relationships and things to keep track of decreases. I'll use StateAdapt, which is more unidirectional than Redux.

Collapse
 
krymancer profile image
Junior Nascimento

Anything that uses redux is already doomed

Collapse
 
mfp22 profile image
Mike Pearson

Sure Jan

Collapse
 
the_riz profile image
Rich Winter

Something I notice is that you may be picking out only one value, but you are always returning the entire state from the getter function. I get that you're not working on it any more, but I'd look up "dependency injection" to specify the data you want in the getter function.

Collapse
 
lnahrf profile image
Lev Nahar

I agree, it can definitely be improved (in more than one way).
Before writing this article I went over the code for a few minutes and noticed some issues.
I don't think I will be working on it, but if you really feel like it I don't mind letting others contribute. (Or we can just leave it as it is and take mental notes, lol).

Collapse
 
steeve profile image
Steeve

I get PTSD just thinking about all the unnecessary complications - dispatchers, reducers, middleware! I just wanted to declare some variables, please make it stop.

I felt the same haha. We just want something simple, 0 deps, and performant 🤷‍♂️

Good article; keep building and creating!

Collapse
 
adaptive-shield-matrix profile image
Adaptive Shield Matrix • Edited

Jotai and Zustand - both manage to be simple, 0 deps and performant.
And they scale as good as Redux as well.

Collapse
 
steeve profile image
Steeve

Thanks for sharing!

Collapse
 
lnahrf profile image
Lev Nahar

Thanks for the support !

Collapse
 
pvivo profile image
Peter Vivo

qwik framework is also use Proxy behind of scene with useStore , this is one of main reason why I preferred compared react. But my daily based jobs depend on react and I think for a couple of years React stay as leading framework/library for UX.

Collapse
 
lnahrf profile image
Lev Nahar

Heard about qwik, I'll have to check it out!

Collapse
 
fridaycandours profile image
Friday candour

💪💪💯. State management libraries don't have to be so complicated.

For the react thing, i am one of the the software engineer behind the web development framework.
github.com/FridayCandour/cra...

Don't forget to share.

Collapse
 
slobodan4nista profile image
Slobi

I wonder why a Proxy is not built into the JS object, it is a bit limiting. There is a need, as in the Pisma project, which essentially made it but with an extra build step. Python has setattr and getattr for this purpose.

Collapse
 
lnahrf profile image
Lev Nahar

Yea, there is definitely a need. I would actually love to see that in the future.

Collapse
 
slobodan4nista profile image
Slobi

By the way I like your lib style, JS with d.ts 🤩 interesting and original approach, well done 👏 I like that you have subscription implemented I would put more thought into definig subscriptions, it looks a bit ambiguous.

Thread Thread
 
lnahrf profile image
Lev Nahar

Thanks! I honestly dislike TS quite a lot but it has it’s uses. The subscription model is definitely ambiguous, far from perfect. Honestly if I were to rewrite it today I’d probably do it differently. Either way, this lib is more of a novelty than anything at this point 🙂

Collapse
 
manishmehra profile image
Manish Mehra

To showcase your passionate projects, you have to land on an interview first and how do you that in 2023? 😆

Nice take. Passion will push you far in programming world. Keep building things and improving your craft. The job will find you.

Collapse
 
lnahrf profile image
Lev Nahar • Edited

Haha yeah, you sure have to land an interview first.
I think that it is really hard for beginners and graduates, for sure. But do what you have to do, upsell yourself. Send messages to employees, to HR, don't be shy! Be professional and always interact with other people in the community.

Once you get to somebody on the phone, act like you are the most sophisticated web developer out there.

Just make sure you do not lie, and remember you do have to know enough to give off a confident vibe, but when you get the first offer it's much, much easier.

Collapse
 
dsaga profile image
Dusan Petkovic

You never know, maybe it becomes popular one day :)

Do you think it can be done by removing the additional createState function, and just initialize it with createVSSM

What do you think about me contributing just for the fun of it?

Collapse
 
lnahrf profile image
Lev Nahar • Edited

Feel free to, could be fun 🙂
I think it’s possible!

Collapse
 
zryru profile image
Jorge Del Castillo

Amazing article

Collapse
 
karishmashukla profile image
Karishma Shukla

Great work 💯

Collapse
 
lnahrf profile image
Lev Nahar

Thank you!

Collapse
 
mohithgupta profile image
K.Mohith Gupta

Any plans on going deep dive to explain the implementation? Would really love to know as I can't seem to understand most of the logics haha ;)

Collapse
 
lnahrf profile image
Lev Nahar

I wouldn’t mind writing such a post (I have to refresh my memory as well, haha). Do you think others will also find it useful?

Collapse
 
iskandarreza profile image
Iskandar Reza

I would find it useful. As a matter of fact just a couple days ago I was looking for a super simple state manager to use in a project I'm working on. I ended up just using session storage for now coz that was even simpler for the task I had, but I might need to upgrade to a better system later.

Collapse
 
mohithgupta profile image
K.Mohith Gupta

Of course, this might help us understand the basics of how state management actually works. Instead of blindly using it, we should understand it I believe.

Collapse
 
christianpaez profile image
Christian Paez

I like this a lot more than Redux and Context API from what I read in your post, I wonder how it works implemented into a React UI, Great job!

Collapse
 
michthebrandofficial profile image
michTheBrandofficial

😭😭 Exactly 💯. State management libraries don't have to be so complicated.

Great article bro. Also, u built a state management library myself github.com/michTheBrandofficial/Ni...

Actually a full framework here

Collapse
 
niscolinx profile image
Igboanugwo Collins

Thank you very much, you just motivated me

Collapse
 
lnahrf profile image
Lev Nahar

Glad to hear that! Go build awesome things!

Collapse
 
toth2000 profile image
Tothagata Bhattacharjee

Great article!! 💫💫

Collapse
 
dali_ayadi profile image
Mohamed Ali Ayedi

Can use it with localstore or AsyncStore ?

Collapse
 
lnahrf profile image
Lev Nahar

I bet you can, but I wouldn't recommend using this, since this hasn't been maintained since it was created back in 2022.

Collapse
 
dali_ayadi profile image
Mohamed Ali Ayedi

Okay, thanks for sharing!

Collapse
 
mfp22 profile image
Mike Pearson

Camel case should apply to acronyms as well.

innerHTMLJSON
innerHtmlJson

InnerHTML should have been innerHtml.

Collapse
 
mitch1009 profile image
Mitch Chimwemwe Chanza

😄 you are damn right but our www central hub made it impossible

Collapse
 
lnahrf profile image
Lev Nahar

You are welcome to contribute!

Collapse
 
wakywayne profile image
wakywayne

I will use it!

Collapse
 
kujian profile image
前端开发博客

funning share, can I translate it to Chinese? I will keep its permalink and author..

Collapse
 
lnahrf profile image
Lev Nahar • Edited

Sure! Can you share with me the URL of the translation when you have it?