Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

JavaScript Joel
JavaScript Joel

Posted on • Edited on

     

Introducing Pico Redux - The faster smaller Redux!

Out of necessity and curiosity, I have created the smallest possible implementation of Redux which comes in at only 95 bytes (minified).

I frequently have needed a stripped down bare bones Redux without all those bells and whistles.

Redux itself is already a pretty small library at 1.6k (gzipped) but I was really looking for something that only had the weight of a single function. Thus Pico Redux was born.

The core Redux functionality itself is very minimal. This is all you need to recreatecreateStore:

constcreateStore=(reducer,state=reducer(void0,{type:'@@init'}))=>({dispatch:action=>(state=reducer(state,action),action),getState:()=>state})
Enter fullscreen modeExit fullscreen mode

I typically don't need middleware (unless I am in React-land). I also rarely need the subscribe functionality. Just the basic store is usually good enough.

But because I do occasionally use these features, I created Pico Redux with a modular design so these features can still be added... but only when necessary so you only pay (in bytes) for what you use.

// basic storeconststore=createStore(reducer)// store with subscribeconststore=withSubscribe(createStore)(reducer)// store with middlewareconststore=withMiddleware(createStore)(reducer)// kitchen sinkconststore=withMiddleware(withSubscribe(createStore))(reducer)
Enter fullscreen modeExit fullscreen mode

The interface is kept the same as Redux so you should already be familiar with how to use it:

import{createStore}from'pico-redux'constinit=0constreducer=(state=init,{type,value})=>type==='INC'?state+value:stateconststore=createStore(reducer)store.dispatch({type:'INC',value:100})store.getState()//=> 100
Enter fullscreen modeExit fullscreen mode

Try it out, give it a spin, let me know what you think!

Github:https://github.com/joelnet/pico-redux

Inspired by:Redux: Implementing Store from Scratch - Dan Abramov

I'm currently available for part time contract work (C#, JavaScript, React). Hit me up onTwitter orlinkedin to get ahold of me.

My articles show massive Functional JavaScript love. If you need more FP, follow me here or on Twitter@joelnet!

Cheers!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
  • Joined

More fromJavaScript Joel

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp