Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 28, 2021 01:16 pm GMT

Introducing ngx-react : Angular React interoperability without pain.

Have you ever wanted to use React components in an Angular application ? Or to start migrating an Angular app to React component-by-component ? Or simply use both at the same time ?

I developped ngx-react which allows you to do just that with ease.

It enables you to use your Angular components in React, and vice versa, quite transparently, and without boilerplate.

Setup

Just declare a bridge, somewhere in your application:

import { NgxReactBridge } from 'ngx-react';export const bridge = new NgxReactBridge()    // bridge options:    .addProvider(/** Add a global react provider here */);

Use in

Then, to use an Angular component in React, just import it like that:

const AsReact = bridge.toReact(MyAngularCommonent);// use it   <AsReact prop={whatever} />

The generated component will take as props all the @Input()s of your component, and all the @Output()s, mapped to functions. (i.e. @Output() onThing: EventEmitter<string> will be mapped to prop onThing: (event: string) => void.

Use in

To use a React component in Angular, you'll have to do a tiny bit more work... for instance if you have a React component:

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

Then magically convert it to its Angular counterpart like that:

@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   @Input()  data!: string;  @Output()  dataChange = new EventEmitter();}

(the Angular compiler doesnt allow to build dynamic components... so we have to declare them statically)

Services

Just inject Angular services in React like that:

const service = useService(MyAngularService);

Wrapping up

I'll write more about this when I have more time But please share your thought if you have some :)

More details on the ngx-react repository


Original Link: https://dev.to/oguimbal/introducing-ngx-react-angular-react-interoperability-without-pain-23k

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To