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
Post a Comment