New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

react-observable-hooks

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-observable-hooks

react library that allows to create and subscribe to observables, ideally for sharing state across differents components

latest
Source
npmnpm
Version
1.0.5
Version published
Maintainers
0
Created
Source

About the library

This library is experimental, avoid using it on production unless you take responsability to fix the bugs

How to Use

1. Using useObservable Hook

The useObservable hook allows you to subscribe to an observable and get its state and status in your React component.

import React from 'react';
import { useObservable } from 'react-observable-hooks';

const MyComponent = () => {
    const { state, dispatch } = useObservable('myObservable', 'initialValue');

    return (
        <div>
            <button onClick={() => dispatch('newValue')}>Update Observable</button>
            <p>the new value is: {state}</p>
        </div>
    );
};

2. Using useSubscribeObservable Hook

The useSubscribeObservable hook allows you to subscribe to an observable and execute a handler function when the observable updates.

import React from 'react';
import { useSubscribeObservable } from 'react-observable-hooks';

const MyComponent = () => {
    useSubscribeObservable('myObservable', (value) => {
        console.log('Observable updated with value:', value);
    });

    return <div>Check the console for updates</div>;
};

3. Using createEffectWithTrigger

The createEffectWithTrigger function allows you to create an effect that triggers when a specific observable updates. Always use it outside the components

import React from 'react';
import { useObservable, createEffectWithTrigger } from 'react-observable-hooks';

createEffectWithTrigger('triggerObservable', 'targetObservable', async (val) => {
    // Perform some async operation
    const result = await fetchSomeData(val);
    return result;
});

const TriggerComponent = () => {
    const { state, dispatch } = useObservable('triggerObservable', 'initialValue');

    return (
        <div>
            <button onClick={() => dispatch('newValue')}>Update Observable</button>
        </div>
    );
};
const TargetComponent = () => {
    const { state } = useObservable('targetObservable', 'initialValue');

    return (
        <div>
            data fetched by the effect {state}
        </div>
    );
};

4. Using createDispachableEffect

The createDispachableEffect function allows you to create an effect that can be dispatched manually. Always use it outside the components

import React from 'react';
import { createDispachableEffect } from 'react-observable-hooks';

//always create effects outside components
const dispatchEffect = createDispachableEffect('targetObservable', async (val) => {
    // Perform some async operation
    const result = await fetchSomeData(val);
    return result;
});

const Dispacher = () => {
    return <button onClick={() => dispatchEffect('someValue')}>Run Effect</button>;
};

const Subscriber = () => {
    const { state, isLoading, isError, isResolved } = useObservable('targetObservable', 'initialValue');

    return (
        <div>
            {isLoading && <p>Loading...</p>}
            {isError && <p>Error occurred</p>}
            {isResolved && <p>Data fetched: {state}</p>}
        </div>
    );
};

This documentation provides an overview of how to use the provided observable functions and hooks in your React components.

FAQs

Package last updated on 19 Feb 2025

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