Reductive, like redux, is a predictable state container for Reason applications. Its scope goes beyond that of managing state for a single component, and concerns the application as a whole.
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.
A recent release of Node LTS should be sufficient.
Installation of this module can be achieved using the npm
command like so:
$ npm install reasonml-community/reductive
See the examples
directory for several working examples using reductive. The
basic
example is console logging only. The immutable
example may be broken
due to an API incompatibility. The render
example demonstrates the
effectiveness of the Lens
, in that only the components whose state has changed
will be re-rendered; turn on the "highlight updates" option in React Devtools to
see the effect. The todomvc
example shows the use of reducer components along
with reductive. While the todomvc example looks a lot like those of the todomvc
project, it does not conform to the todomvc application
specification, instead focusing on demonstrating the usefulness of reductive.
Start by cloning this repository, then get everything installed and built:
$ npm install
$ npm run build
$ npx webpack
You can then open any of the HTML files in the test
folder within your browser.
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.
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.
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 code behind Reductive is incredibly simple. The first 40 lines include the entire re-implementation of redux. The next ~40 lines are a re-implementation of the react-redux library (without the higher-order component connect style implementation). The code is short and relatively simple to follow.