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

dipole-react

v2.1.1

Published

React bindings for Dipole observable state management library

Downloads

29

Readme

dipole-react

React bindings for dipole observable library.

The bindings require React from version 16.8.0, when hooks were introduced. Hooks binding implementation is automatically used for functional components, though class-based components are supported too.

Usage

In oreder to make component rerender on observable/computed value change, you need to wrap it into observer funciton. Alternatively, you can use useObservable hook to achieve the same behaviour without wrapping the component.

Example

Open in codesandbox

import React from "react";
import ReactDOM from "react-dom";
import { action, observable, makeObservable } from "dipole";
import { observer } from "dipole-react";

class CounterModel {
  count = observable.prop(0);

  constructor() {
    makeObservable(this);
  }

  inc = action(() => (this.count += 1));
  dec = action(() => (this.count -= 1));
  reset = action(() => (this.count = 0));
}

const Counter = observer(({ model }) => {
  return (
    <div>
      Counter is: {model.count}
      <button onClick={model.inc}>+</button>
      <button onClick={model.dec}>-</button>
      <button onClick={model.reset}>Reset</button>
    </div>
  );
});

const counterModel = new CounterModel();

ReactDOM.render(
  <Counter model={counterModel} />,
  document.getElementById("root")
);

Example with useObserver:

import { useObserver } from "dipole-react";

function Counter({ model }) {
  return useObserver(() => {
    return (
      <div>
        Counter is: {count}
        <button onClick={model.inc}>+</button>
        <button onClick={model.dec}>-</button>
        <button onClick={model.reset}>Reset</button>
      </div>
    );
  });
}

API

observer(component)

Creates a reactive version of React component that subscribes to observable/computed values accessed in the component and re-renders on their changes.

From dipole-react version 2.0.0 the function works only with functional components. For class components see observerClass

Usage:

// With anonimous component
const Component = observer((props) => <SomeJSX />);

// With named function component (better for React DevTools):
const Component = observer(function Component(props) {
  return <SomeJSX />;
});

// Or, just add `displayName` to it:
Component.displayName = "Component";

// For `React.forwardRef` components it must be applied to render function first:
const RefForwardingComponent = React.forwardRef(
  observer((props, ref) => {
    return <SomeJSX ref={ref} />;
  })
);

// The same goes for `React.memo` components:
const MemoComponent = React.memo(
  observer((props) => {
    return <SomeJSX />;
  })
);

useObserver(observerFn)

Hook-style version of observer. Executes observerFn, tracks all accesses to observable/computed values inside of it and returns its result.

Usual React hooks (useCallback, useMemo, etc) can be used both inside and outside of observerFn, but if you use ESLint, you might want to write them outside in order to make it happy.

Usage:

// Can be used to wrap whole render function
function Component({ model }) {
  return useObserver(() => {
    return <SomeJSX value={model.value} />;
  });
}

// Or wrap only necessarily observable access
function Component({ model }) {
  const value = useObserver(() => model.value);

  return <SomeJSX value={value} />;
}

// Can be used inside `React.memo` and `React.forwardRef` components as well
const MemoComponent = React.memo((props) => {
  return useObserver(() => {
    return <SomeJSX />;
  });
});

const RefForwardingComponent = React.forwardRef((props, ref) => {
  return useObserver(() => {
    return <SomeJSX ref={ref} />;
  });
});

observerClass(classComponent)

Creates a reactive version of classComponent. The argument must be of React.Component or React.PureComponent type.

Usage:

const Component = obseverClass(
  class Component extends React.Component {
    render() {
      const { model } = this.props;

      return <SomeJSX value={model.value} />;
    }
  }
);

License

MIT

Author

Eugene Daragan