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

react-use-observe-changes

v3.0.2

Published

The useObserveChanges is a custom React hook that observes changes in specific fields and updates the observed state. It is useful for tracking and managing the state of multiple fields in a React component efficiently.

Downloads

381

Readme

useObserveChanges

npm version

The useObserveChanges is a custom React hook that observes changes in specific fields and updates the observed state. It is useful for tracking and managing the state of multiple fields in a React component efficiently.

Installation

It is needed to install react-use-observe-changes via npm:

npm install react-use-observe-changes

Usage

To use useObserveChanges, simply import the hook into your component:

Basic Example

import React from 'react';
import useObserveChanges from 'react-use-observe-changes';

const MyComponent = () => {
    const { observedFields, observeIt } = useObserveChanges();

    const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
        observeIt(e.target.name, e.target.value);
    };

    return (
        <div>
            <input name="firstName" onChange={handleChange} />
            <input name="lastName" onChange={handleChange} />
            <pre>{JSON.stringify(observedFields, null, 2)}</pre>
        </div>
    );
};

export default MyComponent;

API

useObserveChanges

Hook that observes changes in specific fields and updates the observed state.

Returns

  • observedFields: An object containing the state of the observed fields.
  • observeIt: A function to observe changes in a field.

Example

const { observedFields, observeIt } = useObserveChanges();

// Observe changes in a field
observeIt('fieldName', 'on');

// Observe changes in a field with a specific value
observeIt('fieldName', newValue);

// Access the state of observed fields
console.log(observedFields);

Observations

  • The state is stored in memory and will persist as long as the component using this hook is mounted.
  • There is no explicit limit to the number of fields that can be observed, but excessive use may impact performance.
  • When the component unmounts, the state will be cleared.

Example

observedFields

// Initial state
const [observedFields, setObservedFields] = useState<{ [key: string]: any }>({});

// After observing changes in a field
observeIt('fieldName', 'newValue');
console.log(observedFields); // { fieldName: 'newValue' }

observeIt

Function to observe changes in a field.

Parameters

  • key: The name of the field to be observed.
  • value: The value to be observed. If it is 'on', the value will be toggled.

Example

// Observe changes in a field with a specific value from an event
observeIt('lastName', e.target.value);

Code Explanation

const observeIt = (key: string, value: any) => {
    if (value === 'on') {
        value = !observedFields[key];
    }
    const newObject = {
        // Spread operator to include all existing observed fields
        ...observedFields,
        // Add or update the field with the new value
        [key]: value
    };
    setObservedFields(newObject);
};
  1. Spread Operator (...observedFields):
  • What happens: The spread operator (...) is used to copy all properties from the observedFields object to the new newObject.
  • For items not found: If the key (key) is not present in observedFields, it will be added to the new newObject with the provided value (value).
  • For items already there: If the key (key) is already present in observedFields, the existing value will be overwritten by the new provided value (value).
  1. Add or Update the Field ([key]: value):
  • What happens: The key (key) is added or updated in the new newObject with the provided value (value).
  • For items not found: The key (key) will be added to the new newObject with the provided value (value).
  • For items already there: The key (key) in the new newObject will have its value updated to the new provided value (value).

Conclusion

The useObserveChanges hook is a useful tool for observing and managing changes in specific fields within a React component. It provides a simple way to track the state of multiple fields and react to changes efficiently.