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

@rx-controller/react

v1.0.6

Published

React hooks and bindings for rx-controller

Downloads

4

Readme

@rx-controller/react

This package contains React and React-Native bindings and hooks for @rx-controller/core.

Table of Contents

Installation

To install the library just run the command

npm install @rx-controller/core @rx-controller/react

or if you are using yarn

yarn add @rx-controller/core @rx-controller/react

Extensibility

During development it occured to me that I cannot predict every possible usecase, so I decided to add strategies that are to be specified in the provider. When I say strategies, I mean the way the useController hook works.

For example, if you are creating a vanilla React project the default strategy specified will suffice. But, if you are developing a React Native project using react-navigation the default strategy till not work since it doesn't respect the library's lifecycle methods. In order to use this library with react-navigation you need to use the @rx-controller/react-navigation-plugin.

You can create your own strategies by creating a class that implements the Strategy class like this.

import { Controller, ClassLike, Emitter, State } from "@rx-controller/core";
import { Strategy, UseControllerOptions, useStore } from "@rx-controller/react";

export class MyStrategy implements Strategy {
  useController<TController extends Controller<any, any>>(
    symbol: ClassLike<TController>,
    options?: UseControllerOptions<TController>
  ): [State<TController>, TController] {
    const {
      // This options specifies wether or not to subcribe to the state
      subscribe = true,
      // This callback will be called when the component mounts
      onMount = (controller: TController) => null,
      // This callback will be called when the component unmounts
      onUnmount = (controller: TController) => null,
      // This callback listens for events emitted by the controller and handles them.
      // The listener must return a callback that removes the listeners it added.
      listener = (emitter: Emitter) => () => null,
      // This callback specifies the condition which must be me in order to update the component
      shouldUpdate = (prev: TState, next: TState) => true,
    } = { ...options };
    ...
    return [state, controller];
  }
}

Configure your Project

To integrate this library to your React or React-Native project you first need to configure it to support decorators and metadata emition. For React I suggest you take a look at craco or if anyone has a better way I would love to hear it.

For React-Native you need to either create or modify your babel configuration to use the babel-plugin-transform-typescript-metadata plugin. Since they explain its installation and configuration in such length I suggest you take a look at their page.

Usage

In order to use the hooks provided by this library you need to wrap your application in the provider passing the store and strategy like this

// store.ts
import { Store } from "@rx-controller/core";

export const store = new Store(...);

// App.tsx
import { Provider as StoreProvider } from "@rx-controller/react";
import { store } from "store";
...

export default App() {
  ...
  return (
    <StoreProvider store={store}>
      ...
    </StoreProvider>
  );
}

Then in every component inside the provider you can use all the hooks this library provides.