Have you ever wanted to render a Preact component inside a React project?
It's hard, right? First of all, why? Because. Second of all, how do you reconcile the two different implementations of JSX?
Preact and React are pretty similar, you see. Both are based on components, both leverage JSX to make your life easier, and for the most part, they look interchangeable.
// React componentimport React from 'react';const Header => <h1>I am a header</h1>
// Preact componentimport Preact from 'preact';const Header => <h1>I am a header</h1>
Where they differ greatly is what that <h1>I am a header</h1>
compiles to. For React, it's a createElement()
call. For Preact ,it's a h()
call.
And that's where the trouble begins.
Reconciling createElement() and h()
I got this idea from Jason himself.
Set your pragma to `h` and conditionally assign h within the scope you want:
โ Jason Miller ๐ฆโ (@_developit) February 26, 2018
/** @jsx h */
import React from 'react'
import { h } from 'preact'
const WithReact = () => {
let h = React.createElement;
return <div>react</div>
}
const WithPreact = () => (
<div>preact</div>
)
Here's how it looks in code ๐
There are 4 parts to this:
- Tell Babel to use
h
for the JSX function. That's the/** @jsx h */
magic comment at the top. - Import both React and Preact. You'll need both.
- Make a Preact component. Or take one from somewhere else. It doesn't matter. Whatever you want :)
- Create a
<Wrapper />
React component
That <Wrapper />
component is where the magic happens.
The render()
method sets h
to React.createElement
and outputs an anchor div. Setting h
ensures that this part of JSX compiles into React's createElement
calls. This makes the component integrate seamlessly with the rest of your project.
Then we hook into the component lifecycle with componentDidMount
and componentDidUpdate
. We call renderPreact
in both of them to ensure our wrapper component always ends up rendering Preact.
Same as my React D3 blackbox approach for quickly wrapping D3 code in React components.
In renderPreact
, we then use Preact's render()
function to render the Preact component into our anchor div. This works the same way as Preact's normal DOM rendering where you call render(<App />, document.getElementById('root'))
to put your app into a root div.
The JSX compiles into Preact's h()
calls because we didn't mess with the setting, and React refs give us a direct reference to the anchor DOM node.
๐
The benchmark
You can now compare React and Preact side-by-side in my interactive DOM benchmark. I think it's a fair comparison because Preact handles its own internals.
Numbers are kinda high though. Preact is supposed to be faster than React because it's closer to the metal. I wonder if it's running in dev mode ๐ค
Learned something new?
Want to become a high value JavaScript expert?
Here's how it works ๐
Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet ๐right away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.
Start with an interactive cheatsheet ๐
Then get thoughtful letters ๐ on mindsets, tactics, and technical skills for your career.
"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. ๐"
Have a burning question that you think I can answer?ย I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.
Ready to Stop copy pasting D3 examples and create data visualizations of your own? ย Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization
Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.
Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first ๐ฐ on the side with ServerlessReact.Dev
Want to brush up on your modern JavaScript syntax?ย Check out my interactive cheatsheet: es6cheatsheet.com
By the way, just in case no one has told you it yet today: I love and appreciate you for who you areย โค๏ธ