How to run Reactjs in codepen

Codepen is an online JavaScript editor that helps to run any JavaScript (client-side) application quickly.

I was recently trying to run Reactjs in it and was facing a little difficulty to configure it. After a little struggle, I was able to figure out the solution and thus share the same here.

To run React in Codepen, you have to first change the JavaScript pre-processor to Babel, so that everything should work without worrying about compatibility.

Babel pre-processor in Codepen

Then we directly import the React and ReactDom from the ESM (EcmaScript Module loader) online.

import React, { useState, useRef, useEffect } from 'https://esm.sh/react@18.2.0'
import ReactDOM from 'https://esm.sh/react-dom@18.2.0'

And that is it we are good to use React now.
Use react in Codepen