Package detail

rescript-react-update

bloodyowl3kMIT5.0.2

useReducer with updates and side effects!

reason-react, rescript, react

readme

rescript-react-update

useReducer with updates and side effects!

Installation

$ yarn add rescript-react-update

or

$ npm install --save rescript-react-update

Then add rescript-react-update to your bsconfig.json bs-dependencies field.

ReactUpdate.useReducer

type state = int;

type action =
  | Increment
  | Decrement;

[@react.component]
let make = () => {
  let (state, send) =
    ReactUpdate.useReducer((state, action) =>
      switch (action) {
      | Increment => Update(state + 1)
      | Decrement => Update(state - 1)
      },
      0
    );
  <div>
    {state->React.int}
    <button onClick={_ => send(Decrement)}> {"-"->React.string} </button>
    <button onClick={_ => send(Increment)}> {"+"->React.string} </button>
  </div>;
};

Lazy initialisation

ReactUpdate.useReducerWithMapState

If you'd rather initialize state lazily (if there's some computation you don't want executed at every render for instance), use useReducerWithMapState where the first argument is a function taking unit and returning the initial state.

type state = int;

type action =
  | Increment
  | Decrement;

[@react.component]
let make = () => {
  let (state, send) =
    ReactUpdate.useReducerWithMapState(
      (state, action) =>
        switch (action) {
        | Increment => Update(state + 1)
        | Decrement => Update(state + 1)
        },
        () => 0
    );
  <div>
    {state->React.int}
    <button onClick={_ => send(Decrement)}> {"-"->React.string} </button>
    <button onClick={_ => send(Increment)}> {"+"->React.string} </button>
  </div>;
};

Cancelling a side effect

The callback you pass to SideEffects & UpdateWithSideEffect returns an option(unit => unit), which is the cancellation function.

// doesn't cancel
SideEffects(({send}) => {
  Js.log(1);
  None
});
// cancels
SideEffects(({send}) => {
  let request = Request.make();
  request->Future.get(payload => send(Receive(payload)))
  Some(() => {
    Request.cancel(request)
  })
});

If you want to copy/paste old reducers that don't support cancellation, you can use ReactUpdateLegacy instead in place of ReactUpdate. Its SideEffects and UpdateWithSideEffects functions accept functions that return unit.

changelog

5.0.2

Changes:

  • Add compat with React 18 (a1bd63a)

5.0.1

Changes:

  • Add compat with ReScript React 0.11.x (6c0d979)

5.0.0

Fixes:

  • Fixed effect cleanup (1816f51)

4.0.0

Changes:

  • Breaking change: the argument order has been changed to match React's APIs (af9447e)

3.0.2

Changes:

  • Update React & React DOM peer dependencies (44c6959)

3.0.1

Fixes:

  • Fixed bsconfig name (thanks @jasim) (2ebd081)

3.0.0

Changes:

  • Move to ReScript (62665af)
  • Move from reason-react to @rescript/react (62665af)
  • Rename project to rescript-react-update (62665af)

2.0.0

Changes:

  • Make effect management preact-safe (76664b6)

1.0.0

Features:

  • Add ReactUpdateLegacy for uncancellable effects, easing the migration process if copy pasting from record API reducers (c4ccec5)
  • Add useReducerWithMapState API to enable lazy state init (19eee9a)

0.1.1

Changes:

  • Stop exposing fullState

0.1.0

Initial release