React, how to access DOM element in render function from same component


g:

I would like to know what is the best practice for accessing DOM elements inside a render function from the same component. Note that I will render this component multiple times on the page.

E.g

var TodoItem = React.createClass({
    ...
    render:function(){

        function oneSecLater(){
            setTimeout(function(){
            //select the current className? this doesn't work, but it gives you an idea what I want.
            document.getElementsByClassName('name').style.backgroundColor = "red";
                )}, 1000);
        }

        return(
            <div className='name'>{this.oneSecLater}</div>
        )



})
Saba Hassan:

Here, no need to use setTimeout. There are some component lifecycle methods, which componentDidMountare called after rendering. So you can get a reference to the div in that method.

var TodoItem = React.createClass({
  ...
  componentDidMount: function () {
     if(this.myDiv) {
        this.myDiv.style.backgroundColor = "red";
     }
  }
  render:function(){
    return(
        <div className='name' ref = {c => this.myDiv = c}></div>
    );
});

Related


Return DOM element in render function of React component

Sir Piros I have an external library that renders some custom js controls. This library returns DOM elements that can be inserted into the page. I am creating a wrapper for this library in React. I hooked it all up, except I'm not sure how to allow the render

Return DOM element in render function of React component

Sir Piros I have an external library that renders some custom js controls. This library returns DOM elements that can be inserted into the page. I am creating a wrapper for this library in React. I hooked it all up, except I'm not sure how to allow the render

Return DOM element in render function of React component

Sir Piros I have an external library that renders some custom js controls. This library returns DOM elements that can be inserted into the page. I am creating a wrapper for this library in React. I hooked it all up, except I'm not sure how to allow the render

Return DOM element in render function of React component

Sir Piros I have an external library that renders some custom js controls. This library returns DOM elements that can be inserted into the page. I am creating a wrapper for this library in React. I hooked it all up, except I'm not sure how to allow the render

Return DOM element in render function of React component

Sir Piros I have an external library that renders some custom js controls. This library returns DOM elements that can be inserted into the page. I am creating a wrapper for this library in React. I hooked it all up, except I'm not sure how to allow the render

How to access input element from React function

Steven Tigerman I currently have a form that is automatically generated based on the number of "activities" the current user has. Each activity has a name and a value. My goal is to submit these values to the backend to update them. But I don't know how to ref

How to access input element from React function

Steven Tigerman I currently have a form that is automatically generated based on the number of "activities" the current user has. Each activity has a name and a value. My goal is to submit these values to the backend to update them. But I don't know how to ref

How to access input element from React function

Steven Tigerman I currently have a form that is automatically generated based on the number of "activities" the current user has. Each activity has a name and a value. My goal is to submit these values to the backend to update them. But I don't know how to ref

How to access input element from React function

Steven Tigerman I currently have a form that is automatically generated based on the number of "activities" the current user has. Each activity has a name and a value. My goal is to submit these values to the backend to update them. But I don't know how to ref

How to access input element from React function

Steven Tigerman I currently have a form that is automatically generated based on the number of "activities" the current user has. Each activity has a name and a value. My goal is to submit these values to the backend to update them. But I don't know how to ref

Render same component React Router Dom with multiple paths

McClosa I was looking for the easiest way to render the same component but with different paths. I have the following like this both "/"and "/login"rendering the login component. import React from "react"; import { Route, Switch, Redirect } from 'react-router-

Render same component React Router Dom with multiple paths

McClosa I was looking for the easiest way to render the same component but with different paths. I have the following like this both "/"and "/login"rendering the login component. import React from "react"; import { Route, Switch, Redirect } from 'react-router-

How to access DOM element of another component in Angular?

username My application is structured like this: <nav></nav> <router-outlet></router-outlet> <footer></footer> In the router socket, based on routing, I have different components. In some of these pages I need to access the DOM elements of the navigation or f

How to access DOM element of another component in Angular?

username My application is structured like this: <nav></nav> <router-outlet></router-outlet> <footer></footer> In the router socket, based on routing, I have different components. In some of these pages I need to access the DOM elements of the navigation or f