Skip to content

Instantly share code, notes, and snippets.

@obenjiro
Last active May 15, 2024 10:03
Show Gist options
  • Save obenjiro/14c35ce5126d87becf89c22a5c22bb54 to your computer and use it in GitHub Desktop.
Save obenjiro/14c35ce5126d87becf89c22a5c22bb54 to your computer and use it in GitHub Desktop.
migration-example
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import App from './App';
import NewPage from './NewPage';
import OldPage from './OldPage';
import './index.css';
ReactDOM.render(
<Router>
<Switch>
<Route path="/new" component={NewPage} />
<Route path="/old" component={OldPage} />
<Route path="/" component={App} />
</Switch>
</Router>,
document.getElementById('root')
);
import React from 'react';
const NewPage = () => {
return (
<div>
<h1>New Page</h1>
<p>This is the new page.</p>
</div>
);
};
export default NewPage;
import React, { useEffect } from 'react';
const OldPage = () => {
useEffect(() => {
// Check if the other app is already initialized
if (!document.getElementById('other-app-root').hasChildNodes()) {
// Dynamically load and initialize the other React app
const script = document.createElement('script');
script.src = '/other-app/main.js'; // Path to the built other React app's main script
script.onload = () => {
window.initializeOtherApp(); // Assuming the other app exposes this function to initialize itself
};
document.body.appendChild(script);
}
}, []);
return (
<div>
<h1>Old Page</h1>
<div id="other-app-root"></div>
</div>
);
};
export default OldPage;
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
window.initializeOtherApp = () => {
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('other-app-root')
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment