2

What I'm trying to achieve is:

  1. Building simple react app - the template is create react app
  2. Copying output file (main.*.js)
  3. Pasting it in another react app
  4. Importing render function to render the first app into the second one

Simple react app code:

interface Props {
  greeting: string;
}
export module AppModule {
  export const sendGreetings = ({ greeting }: Props) => {
    return `Hello ${greeting}`;
  };
}

Builder file code:

!function(){"use strict";var n;(n||(n={})).sendGreetings=function(n){var e=n.greeting;return"Hello ".concat(e)}}();

Trying to import this file into another app I get this error:

File 'c:/vscode/test-react-app/test-sc-react/src/main.783e0281.js' is not a module.ts(2306)

Which is obvious. I changed the output file manually to:

export function initApp(){"use strict";var n;(n||(n={})).sendGreetings=function(n){var e=n.greeting;return"Hello ".concat(e)}};

It works but the only function that I'm able to access is initApp but not sendGreetings

I've been struggling with this for a while now and I would really appreciate any helpful suggestions

4
  • I think your best bet is to communicate through global window object. in your sub application export any functions or values to window[myvalue] so the global apps, or side apps can access that. Commented Nov 21, 2022 at 15:15
  • It is a bit different, but maybe module federation could suit you: webpack.js.org/concepts/module-federation Commented Nov 23, 2022 at 16:39
  • Do you want to import your react app at runtime or build time? Commented Nov 24, 2022 at 21:14
  • You should look into the concept of micro-frontend which enable you to share code between projects even regardless of the source technology (for example you can even construct your application with different frameworks like React and Angular) Commented Nov 28, 2022 at 8:45

3 Answers 3

2

🎯 Solution #1

You can use an iframe to inject your react app:

<iframe src='path-to-your-app.html'/>

🎯 Solution #2

Go with micro-frontend architecture approach. Where a front-end app is decomposed into individual, semi-independent "microapps" working loosely together.

As a starting point, you can try npx create-mf-app instead of the CRA.

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

Comments

0

I used Bit.dev for my components that are used across multiple applications & there is an article regarding your issue

https://blog.bitsrc.io/sharing-react-components-across-multiple-applications-a407b5a15186

I think it would help.

Comments

0

You can include your js code directly on run time. You can use window.addEventListener to load js/css incoming from an outside source. You just have to append that js to your document on the load event.

Comments

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.