frankwallis / reductive

Redux in Reason

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reductive

A reimplementation of redux in Reason.

Word of Caution

You might not need this library, especially so in a language which provides good enough construction blocks out of the box. ReasonReact already comes with reducers!

However, in the spirit of unifying the Reason community around an authoritative Redux implementation and to avoid churn, we'll polish Reductive to make it production-ready for those who do wish to use it anyway.

Comparisons with Redux

Actions

Redux actions are implemented as plain JS objects. JS objects can be a bit too flexible, and many Redux users rely on standardized shapes for their actions to make sure that middlewares and third party libraries work with them. Reason has language-level support for composing a set of data types which many functions can operate over. They are called variants and you can see how they are used here.

Action Creators

Redux action creators are used as composable ways of generating plain JS objects that match the schema of a specific action. In Reason, the type system does a lot of this work for you, so you can reliably put action types directly in your code and know that things will Just Work™. This has an added performance advantage of shifting that function call to compile time.

Enhancer Composition

Much of the power of redux comes from having a unified API for writing and using middleware or enhancers. They're typically wired together behind the scenes in the applyMiddlewares function. This function doesn't exist in Reductive, because Reason has language-level support for function composition with operators like |> and @@. You can see some example usage here and some of the deprecation messages to get a better understanding of typical usage.

The Source

The code behind Reductive is incredibly simple. The first 40 lines Include the entire reimplementation of redux. The next ~40 lines are a reimplementation of the react-redux library (without the higher-order component connect style implementation). The code is short and relatively simple to follow.

Alternative to Reductive

Hopefully, the above comparisons demonstrated that you might not need an extra-react state management technique at all; passing props down works well in Reason-React, and since props are well-typed, adding/remove them takes seconds. Keep state/props management simple so that you can spend the learning budget elsewhere!

Contributing

Current needs:

  • Potential bug fixes
  • Better examples usages
  • More docs on differences from/similarities with Redux
  • Different examples of/links to simpler state management in Reason

About

Redux in Reason

License:MIT License


Languages

Language:OCaml 87.2%Language:HTML 8.2%Language:JavaScript 4.6%