Giter VIP home page Giter VIP logo

react-ref-compare's Introduction

react-ref-compare

A simple utility for comparing refs in React.js with either refs or DOM Elements.

GitHub issues GitHub forks GitHub stars

Table of Contents

Features

  • Compare DOM and Virtual DOM elements by comparing ref with ref or with DOM Element.
  • Get a unique String representation of the DOM Element or ref Object.

Installation

npm i react-ref-compare

Getting Started

import compareRef, { stringify } from "react-ref-compare";

Usage

1. compareRef ( object1, object2 )

Use this method to compare two objects where each object is either a react ref or a DOM Element. This method returns boolean value.

Note: You cannot compare refs that are not of DOM or Virtual DOM elements

import compareRef, { stringify } from "react-ref-compare";
function CustomTextInput(props) {
  const ref1 = useRef(null);
  const ref2 = useRef(null);
  const ref3 = useRef(3);

  function handleClick(e) {
    //comparing ref with ref
    if (compareRef(ref1, ref2)) {
      //do something
    }

    //comparing ref with domElement
    const domElement = document.elementFromPoint(e.clientX, e.clientY);
    if (compareRef(ref1, domElement)) {
      //do something
    }

    //invalid usage
    compareRef(ref1, ref3);
  }

  return (
    <div>
      <input type="text" ref={ref1} />
      <input
        type="button"
        value="Focus the text input"
        ref={ref2}
        onClick={handleClick}
      />
    </div>
  );
}

2. stringify ( Object )

This method returns a String that can be used to identify a DOM / Virtual DOM element. This type of string can be seen when you log elements in console. This returns a string of the format

//dom element
<div id="container" class="container-fluid outer-box">Some content</div>;

//stringify output -> tagName#id.className
div#container.container-fluid outer-box;

react-ref-compare's People

Contributors

jashgopani avatar

Watchers

 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.