npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reaml

v0.15.0

Published

> A React binding for (OCaml | ReasonML) + BuckleScript with compile time > enforcement of the ["Rules of Hooks"](https://reactjs.org/docs/hooks-rules.html) > ([How?](#how-are-the-rules-of-hooks-enforced-at-compile-time)).

Downloads

14

Readme

Reaml (React + ML) Build Test

A React binding for (OCaml | ReasonML) + BuckleScript with compile time enforcement of the "Rules of Hooks" (How?).

Live Examples with Code | Starter | Opinionated Starter

Overview

The "Hello, World!" of Reaml looks like this:

module R = Reaml

let main = R.h1 [ R.id "hello" ] [ R.string "Hello, world!" ]

let () = main |> R.renderTo "main"

The code above renders <h1 id="hello">Hello, World!</h1> into the first element on the page matching the selector main.

Note: If you want to use ReasonML syntax instead of OCaml, check out the last section for the equivalent ReasonML code.


Components are defined using a syntax extension [@reaml.component "DisplayNameOfComponent"] applied to lets of one argument functions:

module R = Reaml

module Counter = struct
  type props = { initial : int }

  let[@reaml.component "Counter"] make { initial } =
    R.div [] [R.int initial]
end

Components are initialized using simple function calls:

let main = Counter.make { initial = 0 }

Hooks are invoked using a [@reaml] annotation on let expressions:

let[@reaml.component "Counter"] make { initial } =
  let[@reaml] count, setCount = R.useState initial in
  R.div [] [R.int initial]

Here's a full example of a Counter with two buttons, one to increment and one to decrement the value (full source):

module Counter = struct
  type props = { initial : int }

  let[@reaml.component "Counter"] make { initial }=
    let[@reaml] count, setCount = R.useState initial in
    R.div
      []
      [ R.button [ R.onClick (fun _ -> setCount (count - 1)) ] [ R.string "-" ]
      ; R.string " "
      ; R.int count
      ; R.string " "
      ; R.button [ R.onClick (fun _ -> setCount (count + 1)) ] [ R.string "+" ]
      ]
end

Custom hooks are created using a syntax extension [@reaml.hook] applied to funs of one argument. Here's a custom hook that wraps useReducer, invoking any action dispatched twice instead of once.

let[@reaml.hook] useDoubleReducer (reducer, initialValue) =
  let[@reaml] state, dispatch = R.useReducer reducer initialValue in
  let dispatchTwice action =
    dispatch action;
    dispatch action
  in
  state, dispatchTwice

These custom hooks are called in the same manner as the built-in hooks -- using let[@reaml]:

let[@reaml.component "CustomHooks"] make () =
  let reducer state action = state + action in
  let[@reaml] state, dispatch = useDoubleReducer (reducer, 0) in
  R.button [ R.onClick (fun _ -> dispatch 2) ] [ R.int state ]

Full example here.

Quick Start

$ git clone https://github.com/utkarshkukreti/reaml-starter
$ cd reaml-starter
$ yarn install
$ yarn build

This will build /src/Main.ml into the /dist/ directory which you can run by opening /index.html in your browser.

Feel free to copy code from examples into src/Main.ml and recompile.

How are the Rules of Hooks enforced at compile time?

Reaml uses an OCaml syntax extension to enforce them.

This requires annotating components with [@reaml.component], custom hooks with [@reaml.hook], and every use of a hook with let[@reaml].

For [@reaml.hook], the syntax extension appends a dummy argument to the function, the value of which must be of type Reaml.undefined (represented as plain undefined in JS).

For both [@reaml.hook] and [@reaml.component], the syntax extension traverses the top level let expressions and rewrites let[@reaml] expressions, appending the undefined value to the function call on the right.

If you call a hook without let[@reaml], you will get a type check error due to a missing argument.

After all this is done, the syntax extension traverses the whole program and checks whether any of these annotations were not processed and throws an error if it finds any because it means the annotation was incorrectly used.

For more examples, check out the files under /examples. A live demo of all the examples is available here.

ReasonML

Here's the Hello World example in ReasonML:

module R = Reaml;

let main = R.h1([R.id("hello")], [R.string("Hello, world!")]);

main |> R.renderTo("main");

Here's an example of ReasonML code which uses all the three annotations that this library uses:

module R = Reaml;

[@reaml.hook]
let useDoubleReducer = ((reducer, initialValue)) => {
  let[@reaml] (state, dispatch) = R.useReducer(reducer, initialValue);
  let dispatchTwice = action => {
    dispatch(action);
    dispatch(action);
  };

  (state, dispatchTwice);
};

[@reaml.component "CustomHooks"]
let make = () => {
  let reducer = (state, action) => state + action;
  let[@reaml] (state, dispatch) = useDoubleReducer((reducer, 0));
  R.button([R.onClick(_ => dispatch(1))], [R.int(state)]);
};

let main = make();
main |> R.renderTo("main");

For more guidance on how to translate OCaml code into ReasonML, try pasting the OCaml code in the Try ReasonML page or read this guide.

License

MIT