1.0.7 • Published 4 years ago

react-use-singleton v1.0.7

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

React useSingleton

a library for creating singleton services that trigger rerenders by breaking reference identity with proxies

Peer Dependencies:

  • react
  • react-dom
// must be a named class
class Example {
  x = 1;
  constructor(arg) {
    this.x = arg || 1;
  }
}

//this is wrong
const AnonClass = class {};

const App = () => (
  <SingletonProvider singleton={/*create a new instance*/ new Example()}>
    <RestOfTheApp />
  </SingletonProvider>
);
const RestOfApp = () => {
  //same instance as above
  const instance = useSingleton(Example);
  return <span>{instance.x}</span>;
  //return <span>1</span>
};

This is achived via js Proxies to trick React into re rendering when a property on the class Instance is changed

Limitations of this method is there is no way to trigger a rerender based on the set of a computed property or observable behavior. Although you may be able to work around this limitation by introducing a component boundry and pass computed properties down as props

All Singletons are instanciated with a method .invalidate() that will trigger a rerender

Typescript Support

class Example extends Singleton {
  // Singleton is an abstract class used to allow strict types
  x = 1;
  constructor(arg) {
    super();
    this.x = arg || 1;
  }
}

const App = () => (
  <SingletonProvider singleton={new Example()}>
    <RestOfTheApp />
  </SingletonProvider>
);
const RestOfApp = () => {
  // use this generic to configure the type of the return value
  const instance = useSingleton<Example>(Example);
  return <span>{instance.x}</span>;
};
1.0.7

4 years ago

1.0.2

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago