banner 728x90

How to get an element of a reagent component

In Mobile Web Development 16 views
banner 728x90

JSX is an amazing pseudo language for React, and if I am honest, that is what leads me brought to love. So respond a lot. The use of React without JSX is cumbersome and frustrating, while the use of JSX is an easier way to express your code. A disadvantage of JSX, however, is that it makes the access to component elements indirect, if not difficult.

The truth is that the access to the individual elements of a component is actually much easier than most thinking. Let's look at how a component method has access to its own DOM node with JavaScript:

Method 1: reaction sum

react-dumb gives a findDomNode method to find the node of the component:

// Get ReactDOM
import ReactDOM from "react-dom";

// In your component method
class MyComponent expands component 

With ReactDOM.findDOMNode (this) you can get the main node of the widget and from there you can use typical DOM methods:

const node = ReactDOM.findDOMNode (this);

// Obtain child nodes
if (node ​​authority of HTMLElement) 

This mixes a little React and basic JavaScript DOM manipulation.

Method 2: ref

Another method to get DOM nodes is to use ref s; an example use is detailed in my React and autofocus post:

class MyComponent expands component 
}

Adding a ref attribute to the element you want to handle is a more response-oriented approach to treating an element. Both strategies work well, so choose what you prefer!


  •  Making a Twitter card
  •  Making a retroPie on Raspberry Pi & # 8211 ; Graphical Manual

    How to make a RetroPie on Raspberry Pi – Graphical Guide

    Today we can play great games on our super-powered game consoles, PCs, VR headsets, and even mobile devices. Although I like to play new games these days, I long for the retro gaming systems I had when I was small: the original Nintendo …

]
  •  jQuery Random link color animations

    jQuery Random link color animations

    We all know that we can set a link: hover color, but what if we want to add something more dynamics and flair? With jQuery you can not only animate to a specified color, but you can also animate to any color.

    The …

  •  Animated AJAX record deletion using jQuery

    Animated AJAX record deletion using jQuery

    I am a big fan of the WordPress method for removing individual articles. You click the Delete link, the menu item shows a red animation and the item disappears. This way you can achieve that functionality with jQuery JavaScript.

    The PHP – Content and header

    The following excerpt is about the …

<! –

->

banner 728x90
author
Author: 
    Build a user settings store with AWS AppSync
    Build a user settings store with AWS AppSync
    Amazon Cognito Sync is a service that
    Synchronize user settings for Android with AWS AppSync
    Synchronize user settings for Android with AWS AppSync
    Amazon Cognito Sync is a service that
    Script & Style Show: Episode 10: Dojo
    Script & Style Show: Episode 10: Dojo
    In this episode welcome David and Todd

    Leave a reply "How to get an element of a reagent component"

    Must read×

    Top