New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@cinematix/reactor

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cinematix/reactor

useReactor hook for using RxJS with React Hooks

  • 2.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-40%
Maintainers
1
Weekly downloads
 
Created
Source

useReactor

A React Hook for using RxJS with React.

Usage

useReactor(value$ => (
    value$.pipe(
        // Whatever you want to do, but the end result will be passed to the dispatch callback.
    )
), dispatch, state.someValueToWatch);

Example

import { useReducer } from 'react';
import useReactor from '@cinematix/reactor';

const initialState = {
    search: '',
};

function reducer(state, action) {
    switch (action.type) {
        case 'CHANGE':
            return {
                ...state,
                [action.name]: action.value
            };
        case 'RESULTS'
            return {
                ...state,
                results: action.results,
            };
        default:
            throw Error('Unkown Action');
    }
}

function AwesomeSearch() {
    const [state, dispatch] = useReducer(reducer, initialState);

    const handleChange = ({ target }) => {
        const { name, value }
        dispatch({ action: 'CHANGE', name, value, });
    };

    useReactor(value$ => (
        value$.pipe(
            // Whatever you want to do, but the end result will be passed to the dispatch callback.
        )
    ), dispatch, state.search);

    return (
        <div>
            <label htmlFor="search">Search</label>
            <input type="text" name="search" id="search" value={state.search} onChange={handleChange} />
            <ul>
                {state.results.map(({ value, label }) => (
                    <li key={value}>{label}</li>
                ))}
            </ul>
        </div>
    );
}

API

useReactor(
    reaction: (value$: Subject) => Subject,
    dispatch: (value: any) => void,
    input?: any | Array<any>
): Subject
  • reaction is a function that returns an instance of Subject
  • dispatch is a function that is called with the result of the reactor. It is the callback to Subject.subscribe(). It is best to end the observable sequence with action objects, but anything can be returned.
  • input is a single item or an array of items of data to emit. When any of the items have changed, the entire list will be emitted. Internally, this value is converted to an array (or used as is) and passed to useEffect().

The Subject that is returned can be used to emit values manually:

const { next } = useReactor(value$ => value$, value => console.log(value));
next('hello!');
// hello!

Since the reactor only needs an instance of Subject a different subject can be returned:

useReactor(() => new BehaviorSubject('hello!'), value => console.log(value));
// hello!

Migrating from 1.x

  • The third parameter has been changed. An input of a scalar will emit a scalar, an input of an array will emit an array. In most instances changing useReactor(searchReactor, dispatch, [state.search]) to useReactor(searchReactor, dispatch, state.search) is sufficient.

Keywords

FAQs

Package last updated on 12 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc