Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getElementById in React

Getting this error at the moment:

Uncaught TypeError: Cannot read property 'value' of null

I call this in my render function below:

<input type="submit" className="nameInput" id="name" value="cp-dev1" onClick={this.writeData}/> 

I also have tried calling it in here

componentWillMount: function(){         var name = document.getElementById('name').value;       }, 

How can I get the id of an input text field and read that value and ensure it is not null?

I think the DOM is loading after I try to read the element hence why it is null

like image 861
The worm Avatar asked Oct 26 '16 07:10

The worm


People also ask

Can I use getElementById in React?

getElementById() method in React is using refs. To select an element, set the ref prop on it to the return value of calling the useRef() hook and access the dom element using the current property on the ref , e.g. ref. current .

How do you access ID in react JS?

To get the id of the element on click in React: Set the onClick prop on the element to a function. Access the id of the element on the currentTarget property of the event . For example, event.currentTarget.id returns the element's id .

What is getElementById used for?

The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.

How do I get an element in Reactjs?

In React we can access the DOM element using Refs. Refs provide a way to access DOM nodes or React elements created in the render method. Creating Refs: Refs are created using React. createRef() and attached to React elements via the ref attribute.

What is the equivalent of document getElementById in react?

The equivalent of document.getElementById in React is refs. We can assign a ref to an element and then retrieve the element that’s assigned the ref from the ref’s current property. to create a ref with the useRef hook and assign it to myContainer. Then we assign the ref to the div by setting the ref prop of the div to myContainer.

How to get the element in react?

A recommended way for React 16.2 and earlier is to use the callback pattern: and so on... Show activity on this post. For getting the element in react you need to use ref and inside the function you can use the ReactDOM.findDOMNode method.

How to access DOM elements directly in react?

are you trying to access dom elements directly ? I dont recommend using dom elements directly in React but if you are using it anyways then you can do it in way use useRef in parent element then simply use querySelectorAll then use childs with index value of that array to access them directly

How to access component through ID in ReactJS?

Before moving ahead let me inform you that React do not provide any way to access component through id. In html, we reference an element through id and javascript uses getElementById to access it. But, components of react are not html elements. In fact, they are not even the part of DOM.


2 Answers

You need to have your function in the componentDidMount lifecycle since this is the function that is called when the DOM has loaded.

Make use of refs to access the DOM element

<input type="submit" className="nameInput" id="name" value="cp-dev1" onClick={this.writeData} ref = "cpDev1"/>    componentDidMount: function(){     var name = React.findDOMNode(this.refs.cpDev1).value;     this.someOtherFunction(name);   } 

See this answer for more info on How to access the dom element in React

like image 157
Shubham Khatri Avatar answered Sep 17 '22 20:09

Shubham Khatri


You may have to perform a diff and put document.getElementById('name') code inside a condition, in case your component is something like this:

// using the new hooks API function Comp(props) {   const { isLoading, data } = props;   useEffect(() => {     if (data) {       var name = document.getElementById('name').value;     }   }, [data]) // this diff is necessary    if (isLoading) return <div>isLoading</div>   return (     <div id='name'>Comp</div>   ); } 

If diff is not performed then, you will get null.

like image 43
HarshvardhanSharma Avatar answered Sep 17 '22 20:09

HarshvardhanSharma