The "Event" You Use in React isn't Real. 😲⚛️ If you log an event object `console.log(e)` in a React handler, you might notice something strange. It disappears or looks different than a standard browser event. That's because you aren't dealing with the DOM. You are dealing with a 𝐒𝐲𝐧𝐭𝐡𝐞𝐭𝐢𝐜 𝐄𝐯𝐞𝐧𝐭. 𝐖𝐡𝐚𝐭 𝐢𝐬 𝐡𝐚𝐩𝐩𝐞𝐧𝐢𝐧𝐠? Browsers (Chrome, Safari, Firefox) all handle events slightly differently. React doesn't want you to worry about those quirks. So, React created a 𝐰𝐫𝐚𝐩𝐩𝐞𝐫 called `SyntheticEvent`. 𝐖𝐡𝐲 𝐝𝐨𝐞𝐬 𝐭𝐡𝐢𝐬 𝐦𝐚𝐭𝐭𝐞𝐫? 1. 𝐂𝐨𝐧𝐬𝐢𝐬𝐭𝐞𝐧𝐜𝐲: It guarantees that your event code works identically on every browser. 2. 𝐏𝐞𝐫𝐟𝐨𝐫𝐦𝐚𝐧𝐜𝐞: React pools these events to save memory (though this behavior changed slightly in React 17, the wrapper concept remains). 3. 𝐅𝐚𝐦𝐢𝐥𝐢𝐚𝐫𝐢𝐭𝐲: It mimics the native API perfectly. You still use `e.preventDefault()` and `e.stopPropagation()` exactly like you're used to. 𝐓𝐡𝐞 "𝐄𝐬𝐜𝐚𝐩𝐞 𝐇𝐚𝐭𝐜𝐡": Need the 𝑎𝑐𝑡𝑢𝑎𝑙 raw DOM event for a third-party library? You can always access it via: `e.nativeEvent`. Check out the visual guide below! 👇 Have you ever had to use `e.nativeEvent` to fix a bug, or has the Synthetic Event always been enough? #ReactJS #FrontendDevelopment #WebDev #JavaScript #CodingInterviews #BrowserCompatibility
Not only across Browsers but across systems - desktop, mobile or (maybe servers🧐)
You would rarely use event.target, and more often the event.currentTarget which aligns with the this value in a regular event listener.