NA +91-8727899942 thecoderjob@gmail.com

CHAPTER - 36

What is forwardRef

Change DOM in different component.

Here's is the Code !!

// Chlid component
import React,{forwardRef} from "react"
function User(props,ref)
{
    return(
        <>
            <input ref={ref} type="text" />
        </>
    )
}

export default forwardRef(User);

// Parent component 
import React ,{useRef} from "react"
import User from "./User"
function App() {
  let inputRef=useRef(null)
  function updateInput()
  {
    inputRef.current.value="1000";
    inputRef.current.style.color="red"
    inputRef.current.focus()


  }
  return (
    <div className="App">
      <h1>forwardRef in React </h1>
      <User ref={inputRef} />
      <button onClick={updateInput} >Update Input Box</button>
    </div>
  );

}
export default App;

Chapter 35

useRef Hook example - it helpfull for change DOM manupulation.

Previous chapter

Chapter 37

Controlled Component

Next chapter

Get In Touch

NA

thecoderjob@gmail.com

+91-8727899942

Popular Links

© www.thecoderjob.com. All Rights Reserved.               Designed by HTML Codex