All Projects → preactjs → Preact Compat

preactjs / Preact Compat

Licence: mit
🙌 React compatibility layer for Preact.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Preact Compat

react-native-react-bridge
An easy way to integrate your React (or Preact) app into React Native app with WebView.
Stars: ✭ 84 (-90.94%)
Mutual labels:  react-dom, preact
Redux React Starter
DEPRECATED use the new https://github.com/didierfranc/react-webpack-4
Stars: ✭ 137 (-85.22%)
Mutual labels:  virtual-dom, preact
Preact
⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.
Stars: ✭ 30,527 (+3193.1%)
Mutual labels:  virtual-dom, preact
Omi
Front End Cross-Frameworks Framework - 前端跨框架跨平台框架
Stars: ✭ 12,153 (+1211%)
Mutual labels:  virtual-dom, preact
Preact Worker Demo
Demo of preact rendering an entire app in a Web Worker.
Stars: ✭ 204 (-77.99%)
Mutual labels:  virtual-dom, preact
Val
VirtualDOM abstraction layer - give yourself better integration and full control over the DOM with any virtual DOM library that uses a Hyperscript-like API such as React and Preact.
Stars: ✭ 181 (-80.47%)
Mutual labels:  virtual-dom, preact
Virtual Dom
关于Vue,React,Preact和Omi等框架源码的解读
Stars: ✭ 139 (-85.01%)
Mutual labels:  virtual-dom, preact
Hydux
A light-weight type-safe Elm-like alternative for Redux ecosystem, inspired by hyperapp and Elmish
Stars: ✭ 216 (-76.7%)
Mutual labels:  virtual-dom, preact
Vhtml
Render JSX/Hyperscript to HTML strings, without VDOM 🌈
Stars: ✭ 556 (-40.02%)
Mutual labels:  virtual-dom, preact
Graphene
Graphene / Graphene-SGX - a library OS for Linux multi-process applications, with Intel SGX support
Stars: ✭ 614 (-33.76%)
Mutual labels:  compatibility
Refract
Harness the power of reactive programming to supercharge your components
Stars: ✭ 791 (-14.67%)
Mutual labels:  preact
Nerv
A blazing fast React alternative, compatible with IE8 and React 16.
Stars: ✭ 5,409 (+483.5%)
Mutual labels:  preact
Nanomorph
🚅 - Hyper fast diffing algorithm for real DOM nodes
Stars: ✭ 621 (-33.01%)
Mutual labels:  virtual-dom
Htm
Hyperscript Tagged Markup: JSX alternative using standard tagged templates, with compiler support.
Stars: ✭ 7,299 (+687.38%)
Mutual labels:  virtual-dom
Rsuite
🧱 A suite of React components .
Stars: ✭ 6,344 (+584.36%)
Mutual labels:  react-dom
Dyo
Dyo is a JavaScript library for building user interfaces.
Stars: ✭ 924 (-0.32%)
Mutual labels:  virtual-dom
Nwb
A toolkit for React, Preact, Inferno & vanilla JS apps, React libraries and other npm modules for the web, with no configuration (until you need it)
Stars: ✭ 5,429 (+485.65%)
Mutual labels:  preact
Gitalk
Gitalk is a modern comment component based on Github Issue and Preact.
Stars: ✭ 5,778 (+523.3%)
Mutual labels:  preact
Preact Redux Typescript Rollup Starter
Smallest "React-like" + Redux starter EVER - 13KB min&gzip
Stars: ✭ 24 (-97.41%)
Mutual labels:  preact
Backbone.vdomview
VirtualDOM-aware Backbone View
Stars: ✭ 23 (-97.52%)
Mutual labels:  virtual-dom

preact-compat

NPM travis-ci CDNJS

🚨 Note: This module is for Preact 8.x and prior - Preact X includes compat by default. For Preact X, please uninstall preact-compat and replace your aliases with preact/compat.

This module is a compatibility layer that makes React-based modules work with Preact, without any code changes.

It provides the same exports as react and react-dom, meaning you can use your build tool of choice to drop it in where React is being depended on.

Interested? Here's an example project that uses preact-compat to work with an existing React library unmodified, achieving more than 95% reduction in size:

preact-compat-example


Why?

... or really, "why preact"?

React is a great library and a great concept, and has a large community of module authors creating high-quality components. However, these components are tightly coupled to React through the use of generic package imports (example).

Preact is a tiny (3kb) implementation of the core value of React, and maintains a nearly identical API. With a shim like this in place, it is possible to use other React-like libraries like Preact, without forking modules just to change their imports.

There are better long-term ways to solve the coupling issue, like using factory functions that accept named generic methods (not just React DI), as suggested by Eric Elliot. However, since the React community has already authored so many modules in a more explicitly coupled manner, it's worth having a simple short-term solution for those who would like to liberate themselves from library lock-in.


Installation

You need to install preact-compat first through npm:

npm i --save preact-compat

NOTE: You need to have preact already installed, if you don't, install it like so:

npm i --save preact

Usage with Webpack

Using preact-compat with Webpack is easy.

All you have to do is add an alias for react and react-dom:

{
    // ...
    resolve: {
        alias: {
            'react': 'preact-compat',
            'react-dom': 'preact-compat',
            // Not necessary unless you consume a module using `createClass`
            'create-react-class': 'preact-compat/lib/create-react-class',
            // Not necessary unless you consume a module requiring `react-dom-factories`
            'react-dom-factories': 'preact-compat/lib/react-dom-factories'
        }
    }
    // ...
}

Usage with Browserify

Using preact-compat with Browserify is as simple as installing and configuring aliasify.

First, install it: npm install --save-dev aliasify

... then in your package.json, configure aliasify to alias react and react-dom:

{
    // ...
    "aliasify": {
        "aliases": {
            "react": "preact-compat",
            "react-dom": "preact-compat",
            // Not necessary unless you consume a module using `createClass`
            "create-react-class": "preact-compat/lib/create-react-class",
            // Not necessary unless you consume a module requiring `react-dom-factories`
            "react-dom-factories": "preact-compat/lib/react-dom-factories"
        }
    }
    // ...
}

If you want to use a package that has a peer dependency of React and want it to point to preact-compat you’ll need to set Aliasify to be a global transform. This is not achievable by editing package.json, you’ll need to use the Browserify api and include the global option there:

b.transform(aliasify, {
  global: true,
  aliases: {
    'react': 'preact-compat',
    'react-dom': 'preact-compat'
  }
});

Usage with Babel

Using preact-compat with Babel is easy.

Install the babel plugin for aliasing: npm install --save-dev babel-plugin-module-resolver

All you have to do is tell babel to process jsx with 'h' and add an alias for react and react-dom in your .babelrc:

{
    // ...
    "plugins": [
        ["module-resolver", {
        "root": ["."],
        "alias": {
            "react": "preact-compat",
            "react-dom": "preact-compat",
            // Not necessary unless you consume a module using `createClass`
            "create-react-class": "preact-compat/lib/create-react-class",
            // Not necessary unless you consume a module requiring `react-dom-factories`
            "react-dom-factories": "preact-compat/lib/react-dom-factories"
        }
        }]
    ],
    "presets": [
        "react"
    ]
    // ...
}

Usage with Brunch

Using preact-compat with Brunch requires no extra plugins.

In your brunch-config.js you can export an npm object to configure aliases:

// ...
exports.npm = {
  enabled: true,
  aliases: {
    'react': 'preact-compat',
    'react-dom': 'preact-compat'
  }
}
// ...

Once Aliased

With the above Webpack or Browserify aliases in place, existing React modules should work nicely:

import React, { Component } from 'react';
import { render } from 'react-dom';

class Foo extends Component {
    propTypes = {
        a: React.PropTypes.string.isRequired
    };
    render() {
        let { a, b, children } = this.props;
        return <div {...{a,b}}>{ children }</div>;
    }
}

render((
    <Foo a="a">test</Foo>
), document.body);

Use Without Webpack/Browserify

preact-compat and its single dependency prop-types are both published as UMD modules as of preact-compat version 0.6. This means you can use them via a <script> tag without issue:

<script src="//unpkg.com/preact"></script>
<script src="//unpkg.com/prop-types/prop-types.min.js"></script>
<script src="//unpkg.com/preact-compat"></script>
<script>
    var React = preactCompat,
        ReactDOM = preactCompat;
    ReactDOM.render(<h1>Hello!</h1>, document.body);
</script>

You can see the above in action with this JSFiddle Example.


PropTypes

preact-compat adds support for validating PropTypes out of the box. This can be disabled the same way it is when using React, by defining a global process.env.NODE_ENV='production'. PropType errors should work the same as in React - the prop-types module used here is published by the React team to replace PropTypes in React.

PropType validation example output

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].