Migrating from ๐Ÿ…ฐ๏ธ to โš›๏ธ ? ๐Ÿ‘‰ Use React components in Angular, and vice versa

Related tags

React ngx-react
Overview

ngx-react allows you to seamlessy use โš›๏ธ React and ๐Ÿ…ฐ๏ธ Angular components together.

๐Ÿ‘‰ This package will be the a perfect match to migrate from Angular to React.

โญ this repo if you like this package, it helps to motivate me :)

๐Ÿง‘โ€๐Ÿ’ป Sample

Jump to the sample repository for a a working sample (here on Stackblitz)

๐Ÿ“ Setup

1) Prepare your Angular project

A) Install ngx-react:

npm i ngx-react

B) Install React:

npm i react react-dom -S
npm i @types/react @types/react-dom -D

C) Configure typescript so it picks up TSX:

      "jsx": "react-jsx",

(in your tsconfig.json, under the "compilerOptions" section)

D) Add the node_modules/ngx-react/src/* as included in your tsconfig.json compilation:

  "include": [
    "src",  // you should already have this one if you had an "include" section :)
    "node_modules/ngx-react/src/*" // ๐Ÿ‘ˆ  add this
    // [...]
  ],

(NB: If someone has a better solution that this, please tell me... but pre-compilling & publish the source seems to fail the angular build when installing this lib)

2) Declare your bridge

At the root of you project, declare a file bridge.ts :

import { NgxReactBridge } from "ngx-react";

// declare the bridge
export const reactBridge = new NgxReactBridge();

you can OPTINALLY declare there the directives that will be available in your react componetns globaly, such as, for instance:

export const reactBridge = new NgxReactBridge();
    .addDirective('focus', (focus: boolean, _, elt) => setTimeout(() => focus && elt.focus()))

3) Enjoy

You can now use React & Angular together ๐Ÿฅณ

Use ๐Ÿ…ฐ๏ธ Angular components in โš›๏ธ React

Suppose you have an Angular component MyAngularComponent you would like to use in React.

In your component declaration file, just put:

import { reactBridge } from "./bridge";

// [...] MyAngularComponent declaration

// this will be usable in React:
export const MyAngular = reactBridge.toReact(MyAngularComponent, {
  // declares that this component accepts children
  children: true,
});

Then, you'll be able to use this in react:

import {MyAngular} from './my-angular.component';

// use the component, enjoy the types !
const Other = () => <MyAngular input={'whatever'}>;

Use โš›๏ธ React components in ๐Ÿ…ฐ๏ธ Angular

Suppose you have a React component MyReactComponent you would like to use in Angular.

In your component declaration file, just put:

import { reactBridge } from "./bridge";

function MyReactComponent(props: {
  data: string;
  dataChange: (evt: string) => void;
}) {
    // [...]
}

@Directive({ selector: "my-react-component" })
export class MyReactComponent_Angular extends reactBridge.toAngular(
  MyReactComponent
) {

  // a bit of extra work: You will have to map the properties yourself (type compatibility will be ensured by Tyepscript, though)
  @Input()
  data!: string;
  @Output()
  dataChange = new EventEmitter();
}

Then, declare MyReactComponent_Angular in your ng-module, and you'll be able to use your component in Angular :

<my-react-component [(data)]="whatever"></my-react-component>

Access ๐Ÿ…ฐ๏ธ Angular services from โš›๏ธ React

Easy

function MyReactComp() {
  const service = useService(SomeAngularService); // simple, isnt it ?
}

๐Ÿ…ฐ๏ธ Angular outputs handling

Angular outputs are bound to callback props in react.

Meaning that:

@Ouptut() valueChange: EventEmitter<string>;

Will be bound to a react prop:

valueChange: (evt: string) => any;

@Input / @Outputs ๐Ÿ…ฐ๏ธ vs โš›๏ธ React state hooks

When importing an Angular component in React, if your angular component has a matching @Input() and @Output() property pairs, say a value input, and valueChange output, you will notice that ngx-react will add a value$ property (name of the input, with a $ suffix) to the corresponding react type.

This property will be typed as something which is compatible with the useState() react hook. Meaning that, for if you have:

@Input() value: string;
@Ouptut() valueChange: EventEmitter<string>;

Then you will be able to use your component in react like that:

const value = useState("");

return <MyComonent value$={value} />;

... and the value state will be two-way bound with your react component !

(But of course, you can still use the value: string and valueChange: (e: string) => any props that ngx-react will have generated for you, if you prefer so)

๐Ÿ’ฅ TODO / Limits

Currently not supported (todo):

  • Integration with the Angular router
  • Inject children in React that are declared in Angular.
You might also like...

The best JavaScript Data Table for building Enterprise Applications. Supports React / Angular / Vue / Plain JavaScript.

The best JavaScript Data Table for building Enterprise Applications. Supports React / Angular / Vue / Plain JavaScript.

Module SonarCloud Status ag-grid-community ag-grid-enterprise AG Grid AG Grid is a fully-featured and highly customizable JavaScript data grid. It del

Dec 30, 2022

react-dialog is a custom react hook to use a dialog easily

react-dialog react-dialog is a custom react hook to use a dialog easily. Documentation To use react-dialog follow the documentation. useDialog Returns

Mar 29, 2022

Shows how React components and Redux to build a friendly user experience with instant visual updates and scaleable code in ecommerce applications

Shows how React components and Redux to build a friendly user experience with instant visual updates and scaleable code in ecommerce applications

This simple shopping cart prototype shows how React components and Redux can be used to build a friendly user experience with instant visual updates and scaleable code in ecommerce applications.

Feb 1, 2022

๐Ÿ”„ Basic project to start studying React and Typescript. With it you can translate text to binary or morse language. This project addresses the creation of routes and components.

max-conversion Projeto criado para iniciar nos estudos de React e Typescript Basic project to gain knowledge in react Na plataforma รฉ possรญvel convert

Feb 12, 2022

Recoil is an experimental state management library for React apps. It provides several capabilities that are difficult to achieve with React alone, while being compatible with the newest features of React.

Recoil ยท Recoil is an experimental set of utilities for state management with React. Please see the website: https://recoiljs.org Installation The Rec

Jan 8, 2023

React components for efficiently rendering large lists and tabular data

React components for efficiently rendering large lists and tabular data

React components for efficiently rendering large lists and tabular data. Check out the demo for some examples. Sponsors The following wonderful compan

Jan 7, 2023

React components for efficiently rendering large lists and tabular data

React components for efficiently rendering large lists and tabular data

react-window React components for efficiently rendering large lists and tabular data React window works by only rendering part of a large data set (ju

Jan 4, 2023

React UI Components for macOS High Sierra and Windows 10

React UI Components for macOS High Sierra and Windows 10. npm install react-desktop --save Help wanted! I am looking for developers to help me develop

Dec 24, 2022

This command line helps you create components, pages and even redux implementation for your react project

This command line helps you create components, pages and even redux implementation for your react project

react-help-create This command line helps you create components, pages and even redux implementation for your react project. How to install it? To ins

Dec 10, 2022
Comments
  • WORKING STRATEGY!

    WORKING STRATEGY!

    Hey guys, I struggled for like half a day and finally found a proper guide that is just working.

    https://thalava.com/how-to-use-react-web-components-in-angular

    opened by markokhman 1
Owner
Olivier Guimbal
CTO @ https://justice.cool [email protected]
Olivier Guimbal
๐Ÿ““ The UI component explorer. Develop, document, & test React, Vue, Angular, Web Components, Ember, Svelte & more!

Build bulletproof UI components faster Storybook is a development environment for UI components. It allows you to browse a component library, view the

Storybook 75.8k Jan 4, 2023
A React utility belt for function components and higher-order components.

A Note from the Author (acdlite, Oct 25 2018): Hi! I created Recompose about three years ago. About a year after that, I joined the React team. Today,

Andrew Clark 14.8k Jan 4, 2023
Nextjs-components: A collection of React components

nextjs-components A collection of React components, transcribed from https://vercel.com/design. 1 Motivation Blog post from 01/09/2022 Todo's Unit tes

null 94 Nov 30, 2022
Minimal Design, a set of components for Angular 9+

Alyle UI Minimal Design, a set of components for Angular. Docs Install Alyle UI Installation Components Feature State Responsive Docs avatar โœ”๏ธ โœ”๏ธ Doc

Alyle 281 Dec 25, 2022
Tweak React components in real time. (Deprecated: use Fast Refresh instead.)

React Hot Loader Tweak React components in real time โš›๏ธ โšก๏ธ Watch Dan Abramov's talk on Hot Reloading with Time Travel. Moving towards next step React-

Dan Abramov 12.2k Jan 1, 2023
A react component available on npm to easily link to your project on github and is made using React, TypeScript and styled-components.

fork-me-corner fork-me-corner is a react component available on npm to easily link to your project on github and is made using React, TypeScript and s

Victor Dantas 9 Jun 30, 2022
React components and hooks for creating VR/AR applications with @react-three/fiber

@react-three/xr React components and hooks for creating VR/AR applications with @react-three/fiber npm install @react-three/xr These demos are real,

Poimandres 1.4k Jan 4, 2023
we are make our components in story book. So if we add some components you can find document and example of useage in storybook.

we are make our components in story book. So if we add some components you can find document and example of useage in storybook.

๊ณ ์Šค๋ฝ 6 Aug 12, 2022
Providing accessible components with Web Components & Material You

tiny-material Still on developing, DO NOT use for production environment Run well on Google Chrome, Firefox, Chrome for Android, Microsoft Edge (Chrom

HugePancake 11 Dec 31, 2022
JavaScript data grid with a spreadsheet look & feel. Works for React, Angular, and Vue. Supported by the Handsontable team โšก

Handsontable is a JavaScript component that combines data grid features with spreadsheet-like UX. It provides data binding, data validation, filtering

Handsontable 17.4k Jan 1, 2023