Skip to content

AyeshaKhan07/ReactForwardRef

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pngwing com

FORWARD REF IN REACT


image

About

This project was created to study the concept of forward ref in React. React forwardRef is a method that allows parent components pass down (i.e., “forward”) refs to their children. Using forwardRef in React gives the child component a reference to a DOM element created by its parent component. This then allows the child to read and modify that element anywhere it is being used.

Technologies

How To Use

# Clone this repository
$ git clone https://github.com/AyeshaKhan07/ReactForwardRef.git

# Go into the repository
$ cd ReactForwardRef

# Install dependencies
$ npm install

# Run the app
$ npm start

Forward ref is forwarding or passing the reference of the DOM element to the child component. By using forwardRef we can manipulate the element's properties without re-rendering that element. In this project, I created a counter app in which the value is incremented by two methods:

  1. React State
  2. Forward Ref

While running the app, follow the below steps to get the better understanding:

  • Open the console and change the switch to increment by react state.
  • You will see every time you press the increment button, this message got printed "component rendered!".
  • Now change the switch again to increment by forward ref.
  • Now you will see every time you press the increment button, no message got printed on console.

This proves that if we increment the value by state the component will re-render on every increment, but if we increment by forwardRef the component will just update the value without rerendering.

Further Reading

Author

@AyeshaKhan07