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

@custom-react-hooks/use-effect-once

v1.5.1

Published

`useEffectOnce` is a custom hook in React designed to mimic the behavior of `componentDidMount` and `componentWillUnmount` lifecycle methods in class components. It's a modified version of `useEffect` that runs only once when the component mounts.

Downloads

821

Readme

useEffectOnce Hook

useEffectOnce is a custom hook in React designed to mimic the behavior of componentDidMount and componentWillUnmount lifecycle methods in class components. It's a modified version of useEffect that runs only once when the component mounts.

Features

  • Single Execution: The hook executes the provided effect function once upon the component's initial render.
  • Cleanup Capability: It supports an optional cleanup function, returned from the effect, which is called when the component unmounts.
  • SSR Compatibility: As an extension of useEffect, it is naturally compatible with server-side rendering environments.

Installation

Choose and install individual hooks that suit your project needs, or install the entire collection for a full suite of utilities.

Installing Only Current Hooks

npm install @custom-react-hooks/use-effect-once

or

yarn add @custom-react-hooks/use-effect-once

Installing All Hooks

npm install @custom-react-hooks/all

or

yarn add @custom-react-hooks/all

Importing the Hook

The useEffectOnce hook must be imported using a named import as shown below:

Named Import:

import { useEffectOnce } from '@custom-react-hooks/use-effect-once';

This approach ensures that the hook integrates seamlessly into your project, maintaining consistency and predictability in how you use our package.

Usage

import React from 'react';
import { useEffectOnce } from '@custom-react-hooks/all';

const EffectOnceComponent = () => {
  const [fibonacciSequence, setFibonacciSequence] = useState([]);

  const calculateFibonacci = (n) => {
    let sequence = [0, 1];
    for (let i = 2; i < n; i++) {
      sequence[i] = sequence[i - 1] + sequence[i - 2];
    }
    return sequence.slice(0, n);
  };

  useEffectOnce(() => {
    const sequence = calculateFibonacci(5);
    setFibonacciSequence(sequence);
  });

  return (
    <div>
      <p>First {5} numbers in the Fibonacci sequence:</p>
      <ul>
        {fibonacciSequence.map((number, index) => (
          <li key={index}>{number}</li>
        ))}
      </ul>
    </div>
  );
};

export default EffectOnceComponent;

In this example, useEffectOnce is used to perform actions at the mounting and unmounting phases of MyComponent.

API Reference

Parameters

  • effect: A function that will be executed once when the component mounts. This function can optionally return a cleanup function, which will be executed when the component unmounts.

Use Cases

  • Initial Setup: Perform setup operations like fetching initial data or setting up listeners.
  • One-time Calculations: Compute values needed only once during the component's lifecycle.
  • Single API Calls: Make a single API call when a component is rendered for the first time.
  • Non-Recurring Subscriptions: Subscribe to a service or event listener that should only be initialized once.

Contributing

Contributions to enhance useEffectOnce are always welcome. Feel free to submit issues or pull requests to the repository for further improvements.