Coder Social home page Coder Social logo

manipulating-dom-with-refs's Introduction

manipulating-DOM-with-refs

In this read, I learnt:

  • to get a ref to the DOM node. You can do this by importing the useRef hook then initializing your ref with it inside your component. Finally to access the DOM node, you pass the ref to the ref attribute of the DOM node e.g. <section ref={myRef}>. By doing this, the DOM node's reference is stored into myRef.current property. You can then manipulate this DOM node however you like using event handlers & other built in browser API functions that work with it.
  • you can have more than a single ref in a component
  • when dealing with lists that you have no idea how many items are in it & each item needs a ref, the best solution is using a ref callback. It's a function you pass to the ref attribute. Using this, you can access each item's ref by its index or ID
  • react doesn't let components accesss the DOM nodes of other components, not even its own children components. Manually manipulating other component's DOM nodes make your code fragile.
  • if a component wants to expose their DOM node to other components it has to explicitly do so by using React.forwardRef()
  • putting a ref on a custom-made component doesn't work i.e the ref doesn't store the component inside its current property since its React does not let components access other component's nodes. Such components need to use forwardRef() if they want their nodes to be accessed by other components. You do this by adding forwardRef() to the custom functional component's definition
    const MyInput = forwardRef((props, ref) => {
      return <input {...props} ref={ref} />;
    });
  • allowing other components to manipulate your DOM nodes also allows them to change your CSS styles. If you want to restrict this behavior to only manipulating the DOM node, do so by using useImperativeHandle() => it lets you pass your own custom object as the value of the ref to the parent component and instruct react on what to do with that value passed
  • React state updates are usually queued, to override this and have the state update synchronously, import flushSync from react-dom and wrap the logic updating the state with it
  • only use refs when you want to step outside React

manipulating-dom-with-refs's People

Contributors

2kelvin avatar

Stargazers

 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.