0

I made two apps one is for angularjs and one is to react. Now the problem is I include the react build in angularjs app and try to initialize the '' component but when I run the code it says Test is not defined.

Can someone help me with this or give me any idea how I can get out of this problem.

React Component:

import React from 'react';
import ReactDOM from 'react-dom';


  class Test extends React.Component {
    render() {
        return (
          <div>
            Hello
          </div>
        );
      }
   }

angular Js Code:

<!DOCTYPE html>
  <html>
   <head>
    <title></title>
   </head>
    <body ng-app="angular-app">

    <div id="root"></div>

    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular.min.js"></script>

    <script src="react/dist/build.js"></script>

    <script>
        ReactDOM.render(<Test/>, document.getElementById('root'));
    </script>
   </body>
</html>

2 Answers 2

1

Thing is, you cannot use jsx inside browser, jsx is usually transpiled using Babel into JavaScript function calls.

So 1. <Test /> will never work inside browser, you need to Babel that.

  1. You need to expose react to you angularjs, best way I can think of is
// In react, instead of exporting component, export a function that mount component in given HTML


Export default (elm) => ReactDOM.render(<Test/>, elm) 
Sign up to request clarification or add additional context in comments.

Comments

1

You should write the following code in your react app and then load your bundle file in angularJS app.

ReactDOM.render(<Test/>, document.getElementById('root'));

3 Comments

Is their any way to render from AngularJS app?
You can use an iframe element
without iframe any other way?

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.