React JS - Ref

  • Ref is used in class component.

  • Ref is used to manipulate DOM directly.

  • We should avoid using Ref or use it only in emergency cases.

In the following example, on button click, color of input text will change using Ref.

createRef() creates a ref object which can contain arbitrary value. It takes no parameters.






















Output :














Interview Question :

  • Can we use Ref in Functional Component?
    Yes, using useRef Hook


Comments