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

re-scoped-model

v3.0.1

Published

Scoped Model pattern in ReasonReact (but with Hooks)

Downloads

33

Readme

re-scoped-model

Scoped Model pattern in React (but with Hooks), a pure ReasonML implementation of react-scoped-model

NPM JavaScript Style GuideOpen in CodeSandbox

Install

npm install --save re-scoped-model
yarn add re-scoped-model

bsconfig.json

  "bs-dependencies": [
    "re-scoped-model"
  ],

Usage

Creating a model

Models are created by using a hook function that is always called whenever its Provider renders, and must return an object that represents the models' state:

module Counter = {
  type props = int;

  type t = {
    decrement: unit => unit,
    increment: unit => unit,
    count: int,
  };

  let call = (props) => {
    let (count, setCount) = React.useState(() => props);

    let decrement = React.useCallback0(() => {
      setCount((current) => current - 1);
    });
    let increment = React.useCallback0(() => {
      setCount((current) => current + 1);
    });

    {
      count,
      decrement,
      increment,
    };
  };

  let displayName = "Counter";

  let shouldUpdate = (prev, next) => {
    prev == next;
  };
};

module CounterModel = ScopedModel.Make(Counter);

Adding to your component tree

To add the Model to your component tree, simply use the Provider component property:

module App {
  [@react.component]
  let make = () => {
    <CounterModel.Provider props=0>
      <CounterModel.Provider props=100>
        <Count />
        <Increment />
        <Decrement />
      </CounterModel.Provider>
        <Count />
        <Increment />
        <Decrement />
    </CounterModel.Provider>
  };
}

useSelector Hook

To access our model's state, we can use the useSelector hook which accepts the model's reference and a function that receives the current model state, and returns a new value that is derived from the given state. This allows fine-grained and reasonable re-render for the listening component, as the component will only re-render if the transformed value changes every time the model updates. A third optional argument can be provided which accepts a function that compares the previously transformed state from the previous render and the newly transformed state.

For example, in our Count component, we only select the count field of our model record.

module Count {
  [@react.component]
  let make = () => {
    let count = ScopedModel.useSelector(. Counter, state => state.count, None);

    Js.log("Count");

    <p>{ ReasonReact.string(string_of_int(count)) }</p>;
  }
}
module Increment {
  [@react.component]
  let make = () => {
    let increment = ScopedModel.useSelector(. Counter, state => state.increment, None);

    Js.log("Increment");

    <button onClick={_ => increment()}>
      { ReasonReact.string("Increment") }
    </button>;
  }
}
module Decrement {
  [@react.component]
  let make = () => {
    let decrement = ScopedModel.useSelector(. Counter, state => state.decrement, None);

    Js.log("Decrement");

    <button onClick={_ => decrement()}>
      { ReasonReact.string("Decrement") }
    </button>;
  }
}
module IncDec {
  [@react.component]
  let make = () => {
    let (increment, decrement) = Counter.useSelector(state => (
      state.increment,
      state.decrement,
    ), true);

    Js.log("IncDec");

    <React.Fragment>
      <button onClick={_ => increment()}>
        { ReasonReact.string("Increment") }
      </button>
      <button onClick={_ => decrement()}>
        { ReasonReact.string("Decrement") }
      </button>
    </React.Fragment>
  };
}

Other hooks

There are 3 other hooks:

  • useValue: Consumes the model's current state and updates when the model's state updates.
  • useValueOnce: Consumes the model's current state once.
  • useSelectorOnce: Similar to useSelector, consumes and transforms the model's current state once.

Hook Factories

There are 4 built-in functions that are higher-order hooks. These functions are beneficial for stabilizing functional references (e.g. selector functions) to prevent recomputation of internal side effects.

  • createValue
  • createValueOnce
  • createSelector
  • createSelectorOnce

Model Factories

  • MakeNullary - a model with unit props, stabilizing the model from further recomputation whenever the Provider updates props or children.
  • MakeState - a kind of nullary model whose state is that of the React.useState.
module Count = MakeState({
  type state = int;

  let initialState = () => 0;

  let displayName = "Count";
});
  • MakeReducer - a kind of nullary model whose state is that of the React.useReducer.
module Count = MakeReducer({
  type state = int;
  
  type action = 
    | Increment
    | Decrement;

  let initialState = () => 0;

  let reducer = (state, action) => {
    switch (action) {
      | Increment => state + 1;
      | Decrement => state - 1;
    }
  };

  let displayName = "Count";
});
  • MakePropSelector- a kind of model whose props and state are the same.

License

MIT © lxsmnsyc