2

I have to add some angular components to angular project and I found this package. I did the following steps:

  1. npm install react2angular react react-dom prop-types --save into the angular project

  2. Add a directory (in the root project) named src and inside it one index.js with the following code:

    import { react2angular } from "react2angular";
    import React from "react";
    
    function Example() {
      return <button type="button">Click me</button>;
    }
    
    angular
      .module("myModule")
      .component("example", react2angular(Example, ["apiUrl", "token"]));
    
  3. after this I moved in the angular code and I added

    <example api-url="123" token="456"/>
    

Nothing happens. No button appears.

This seems to be made based on the official documentation. Did I do something wrong? Keep in mind that I have no experience with angular, this is why I want to work with react.

8
  • Why are you doing an angular project if you want to work only in react ? Commented Nov 4, 2021 at 9:55
  • 1
    because the angular projec has been made like 5 years ago and I can't do it again because it is super huge. Commented Nov 4, 2021 at 10:03
  • can you show where you use myModule? Most likely, your angular module myModule is not registered. Commented Nov 4, 2021 at 10:16
  • You can't rewrite the app but you can certainly rewrite the component... Commented Nov 4, 2021 at 10:19
  • @yadejo it is a fictitious name.. I think I have to put there the name of the module where I am going to use this component, right? Commented Nov 4, 2021 at 10:27

2 Answers 2

1
1. Create a React component

    import React from 'react';
    const ReactComponent = ({ fooBar, baz}) => (
      <div>
      <span>value: {fooBar} </span>
      <button onClick={baz}>
        Add one
      </button>
      </div>
     );

   export default ReactComponent;

2. Expose it to Angular

    import { react2angular } from 'react2angular'

    angular
      .module('myModule', [])
      .component('myComponent', react2angular(ReactComponent, ['fooBar', 'baz']))

3. Use it in your Angular 1 code

    <my-component
     foo-bar="3"
     baz="'baz'"
    ></my-component> 

https://stackblitz.com/edit/react2angular-multiple-modules-cg9zaw?file=angular-component.js

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This is right, but in my specific case the problems were different. I had to inject it in my gulp file.
-1

Please go through below link, by adding wrapper class you can add react component or react npm plugin in to angular project.

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

points to note

  1. Add wrapper component in appmodule.ts
  2. do npm install after adding react dependencies in package.json

1 Comment

The link you posted is a dead link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.