Seamlessly Render a Preact Component in a React Project [Livecoding]

综合技术 DZone (源链)

Seamlessly Render a Preact Component in a React Project [Livecoding]

DZone’s Guide to

Seamlessly Render a Preact Component in a React Project [Livecoding]

Have you ever wanted to render a Preact component inside a React project? It’s hard, right? Read on to find out how to get it to work.

Swizec Teller

Mar. 13, 18 ·
Web Dev Zone ·

Free Resource

Join the DZone community and get the full member experience.

Read this guide to learn everything you need to know about RPA, and how it can help you manage and automate your processes.

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 component
import React from 'react' ;
const Header => 

I am a header

// Preact component
import Preact from 'preact' ;
const Header => 

I am a header

Where they differ greatly is what that

I am a header

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:

/** @jsx h */

import React from ‘react’

import { h } from ‘preact’

const WithReact = () => {

let h = React.createElement;

return

react

}

const WithPreact = () => (

preact

)

– Jason Miller (@_developit) February 26, 2018

Here’s how it looks in code:

There are 4 parts to this:

  1. Tell Babel to use h for the JSX function. That’s the /** @jsx h */ magic comment at the top.
  2. Import both React and Preact. You’ll need both.
  3. Make a Preact component. Or take one from somewhere else. It doesn’t matter. Whatever you want!
  4. Create a React component.

That 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(, 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…

Get the senior executive’s handbook of important trends, tips, and strategies to compete and win in the digital economy.

您可能感兴趣的

ESLint + Prettier For a Consistent React Codebase Photo by Markus Spiske on Unsplash ESLint + Prettier For a Consistent React Codebase...
React Easy Params: Auto-Sync Your State with the U... React Easy Params :link: Auto synchronize your state with the URL and the LocalStorage. https://circleci.com/gh/solkimicreb/react-e...
Next.js 6.0.1-canary.0 发布,React 应用的后端渲染框架... Next.js 6.0.1-canary.0 发布,React 应用的后端渲染框架 局长 发布于2018年05月06日 收藏 0 【腾讯云】云服务器新购特惠,1核1G 265元/年起! >>>...
Dynamic GraphQL polling with React and Apollo Clie... I recently added a feature to the admin interface on Meteor Galaxy , Meteor Development Group’s hosting platform for Meteor apps, that shows the s...
React Tutorials – Day Three – Componen... This is the third article of the React Tutorials series. Following are the links of my previous articles of this series. React Tutorials - D...
DZone责编内容来自:DZone (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » Seamlessly Render a Preact Component in a React Project [Livecoding]



专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录