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

use-effect-except-mount

v1.0.0

Published

A custom React hook that modifies useEffect hook to skip the effect on mount (first render) and only run when dependency is changed

Downloads

31

Readme

useEffect Except Mount

Customised React useEffect hook that does not run on mount. See demo here.

The problem

useEffect hook allows you to perform side effects with function components. It runs the specified effect whenever its depenencies change.

useEffect(() => {
    // Side effect
}, [...dependencies]);

But it also runs whenever the component mounts. Sometimes that is not desirable. A universal solution to avoid running effect on mount is to use refs. That is exactly what this hook does.

Usage

  1. Installation
  2. Using it in your code

Installation

Hook has a peer dependency on React 17.01. If you use React 16.x, it might be a good idea to upgrade to 17.x. This version has no breaking changes. It also makes your application ready for future major versions.

npm i react use-effect-except-mount

Using it in your code

The hook has exactly same signature as the useEffect hook.

import useEffectExceptMount from "use-effect-except-mount";

const Component: React.FC = () => {
    useEffectExceptMount(() => {
        // Side effect
    }, [...dependencies]);

    // Other rendering logic
};

Source code structure

This section is for developers who wish to work upon the existing source code. The structure is pretty straightforward. Code is written mainly in TypeScript. You can start by cloning the repository and running

npm i react # Getting peerDependency
npm i # Getting all devDependencies

There are three components to the source code:

1. The hook logic

This code resides in src/index.ts. It simply exports a function with same signature as useEffect. Built version resides in lib/index.js.

2. The test cases

This code resides in __test__/index.tsx. Code is with written using Jest.

3. The demo

This is a bundled web application powered by parcel. Source code resides in example folder . Built code resides in docs and is deployed on GitHub pages. Before building the demo, change the *main field of package.json to docs/index.html.

Following are the supported commands:

  1. npm test: Run the test cases
  2. npm run build : Build the hook logic
  3. npm run parcel-build: Build the demo
  4. npm run parcel-start: Run the demo is development mode.