Web Expe

webexpe.com

How to use ref to style in reactjs

By: Rupali Yadav

Web Expe

Imports

Import css from emotion and useRef from react as shown below.

Web Expe

Declare a ref

Create a component with name Card and declare a ref as shown below.

Web Expe

Pass the ref

When a ref is passed to an element in render, a reference to the node becomes accessible at the current attribute of the ref.

Web Expe

Add onClick

Point to the current property of the ref that holds the reference to the card whose background color we want to change on it's click.