All Projects → bloodyowl → Rescript React Update

bloodyowl / Rescript React Update

Licence: mit
useReducer with updates and side effects!

Projects that are alternatives of or similar to Rescript React Update

bs-react-is-visible
A small library that lets you know whether a component is visible on screen or not.
Stars: ✭ 15 (-81.01%)
Mutual labels:  reasonml, reason-react
bs-react-native-vector-icons
ReasonML bindings for react-native-vector-icons
Stars: ✭ 16 (-79.75%)
Mutual labels:  reasonml, reason-react
credt
CRDT-like data structures for building distributed, offline-first applications
Stars: ✭ 32 (-59.49%)
Mutual labels:  reasonml, reason-react
reason-rt-binding-generator
Reason binding generator for react-toolbox
Stars: ✭ 18 (-77.22%)
Mutual labels:  reasonml, reason-react
Reason React Hacker News
hacker news mobile app made with reason react
Stars: ✭ 591 (+648.1%)
Mutual labels:  reason-react, reasonml
onetricks.net
(WIP) kayn-powered (typescript node.js) ReasonReact app presenting you a dashboard of high ELO one trick ponies in League of Legends
Stars: ✭ 13 (-83.54%)
Mutual labels:  reasonml, reason-react
Pragma
Pragma is a self-hosted, open-source, personal note taking app.
Stars: ✭ 39 (-50.63%)
Mutual labels:  reason-react, reasonml
reason-catstagram
🐈 Catstagram made with ReasonReact!
Stars: ✭ 31 (-60.76%)
Mutual labels:  reasonml, reason-react
Reductive
Redux in Reason
Stars: ✭ 405 (+412.66%)
Mutual labels:  reason-react, reasonml
Isolate
Lightweight image browser
Stars: ✭ 284 (+259.49%)
Mutual labels:  reason-react, reasonml
timerlab
⏰ A simple and customizable timer
Stars: ✭ 94 (+18.99%)
Mutual labels:  reasonml, reason-react
Cra Template Rescript Lukin
🐪 Lukin CRA and ReScript Template
Stars: ✭ 18 (-77.22%)
Mutual labels:  reason-react, reasonml
dokusho
Simple Japanese reading stats tracker
Stars: ✭ 12 (-84.81%)
Mutual labels:  reasonml, reason-react
react-multiversal
React components that works everywhere (iOS, Android, Web, Node)
Stars: ✭ 43 (-45.57%)
Mutual labels:  reasonml, reason-react
qnd
Quick and Dirty development builds
Stars: ✭ 19 (-75.95%)
Mutual labels:  reasonml, reason-react
rescript-react-compat
An alternative upgrade path for ReasonReact
Stars: ✭ 17 (-78.48%)
Mutual labels:  reasonml, reason-react
LifeTime
LifeTime app
Stars: ✭ 35 (-55.7%)
Mutual labels:  reasonml, reason-react
Reason-react-hooks
🧶 Some hooks in ReasonML for reason-react that can be useful
Stars: ✭ 14 (-82.28%)
Mutual labels:  reasonml, reason-react
React Recomponent
🥫 Reason-style reducer components for React using ES6 classes.
Stars: ✭ 272 (+244.3%)
Mutual labels:  reason-react, reasonml
Rescript React Native
ReScript bindings for React Native
Stars: ✭ 802 (+915.19%)
Mutual labels:  reason-react, reasonml

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(0, (action, state) =>
      switch (action) {
      | Increment => Update(state + 1)
      | Decrement => Update(state - 1)
      }
    );
  <div>
    {state->Js.String.make->React.string}
    <button onClick={_ => send(Decrement)}> "-"->React.string </button>
    <button onClick={_ => send(Increment)}> "+"->React.string </button>
  </div>;
};

Lazy initialisation

ReactUpdate.useReducer

If you'd rather initialize state lazily (if there's so 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(
      () => 0,
      (action, state) =>
        switch (action) {
        | Increment => Update(state + 1)
        | Decrement => Update(state + 1)
        }
    );
  <div>
    {state->Js.String.make->React.string}
    <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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].