Ref with Example - React Js

 App.js:


import React, {Component, createRef} from "react";
import './App.css'

class App extends Component
{
  constructor()
  {
    super()
    this.inputRef=createRef();
  }
  componentDidMount()
  {
    console.warn(this.inputRef.current.value="10001")
  }
  getVal()
  {
    console.warn(this.inputRef.current.value)
    this.inputRef.current.style.color="red"
    this.inputRef.current.style.backgroundColor="black"
  }
  render()
  {
    return(
      <div className="App">
        <h1>Ref Component</h1>
        <input type="text" ref={this.inputRef}/>
        <button onClick={()=>this.getVal()}>GetRef</button>
      </div>
    )
  }
}

export default App;



Comments

Popular posts from this blog

Send Data Child to Parent Component - React Js

Hide , Show and Toggle in ReactJs

Importance Of Web Content In SEO.