Code Monkey home page Code Monkey logo

use-update-effect's Introduction

useUpdateEffect

version minzipped size downloads

useUpdateEffect is a React hook that mimics the behavior of componentDidUpdate in function components.

You may also like use-update-layout-effect.

Install

  • npm install use-update-effect --save or
  • yarn add use-update-effect

Use

You use the useUpdateEffect the same way you would use the useEffect hook. Provide an effect callback and a dependency list, and the effect callback will only execute when the dependency list updates.

For a behavior exactly the same as componentDidUpdate, provide an empty ([]) or no (undefined) dependency list.

In the following example, there is no alert when the component mounts; but when the username changes, an alert appears.

import useUpdateEffect from 'use-update-effect';

function MyComponent({ username }) {
  useUpdateEffect(() => {
    alert(`Now logged in as ${username}!`);
  }, [username]);

  return <div>{username}</div>;
}

In the following example, a controlled input is allowed to have an in-flight value until "Apply" is clicked. By using an update effect, we override the in-flight value when a new controlled value is provided. This is useful when a controlled value may have more than one controlling component.

import { useState } from 'react';
import useUpdateEffect from 'use-update-effect';

function MyComponent({ onChange, value }) {
  const [localValue, setLocalValue] = useState(value);

  useUpdateEffect(() => {
    setLocalValue(value);
  }, [value]);

  return (
    <>
      <input
        onChange={e => {
          setLocalValue(e.target.value);
        }}
        value={localValue}
      />
      <input onClick={onChange} type="submit" value="Apply" />
    </>
  );
}

use-update-effect's People

Contributors

quisido avatar

Stargazers

Mateusz Bagiński avatar Violet avatar Jesse Holden avatar  avatar Joel Moss avatar  avatar Thomas avatar Richard Scarrott avatar Roman Hossain Shaon avatar Michael Demarais avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.