-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changes for v5 to work with React 18 and React Router's data routers
- Loading branch information
Showing
6 changed files
with
3,485 additions
and
2,083 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,23 @@ | ||
import { FastRender } from 'meteor/communitypackages:fast-render'; | ||
|
||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { BrowserRouter } from 'react-router-dom'; | ||
import React, { StrictMode } from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import { RouterProvider, createRoutesFromElements, createBrowserRouter } from 'react-router-dom'; | ||
|
||
import './version-check'; | ||
|
||
let Provider; | ||
let applyMiddleware; | ||
let createStore; | ||
const renderWithSSR = (routes, { renderTarget = 'react-target' } = {}) => { | ||
if (!Array.isArray(routes)) { | ||
routes = createRoutesFromElements(routes); | ||
} | ||
|
||
/* eslint-disable */ | ||
try { | ||
({ Provider } = require('react-redux')); | ||
({ createStore, applyMiddleware } = require('redux')); | ||
} catch (e) {} | ||
const router = createBrowserRouter(routes); | ||
|
||
const renderWithSSR = (component, { renderTarget = 'react-target', storeOptions } = {}) => { | ||
const AppJSX = <StrictMode><RouterProvider router={router} /></StrictMode>; | ||
|
||
let ReactRouterSSR = () => ( | ||
<BrowserRouter> | ||
{component} | ||
</BrowserRouter> | ||
); | ||
|
||
if (storeOptions) { | ||
const { rootReducer, middlewares } = storeOptions; | ||
const appliedMiddlewares = middlewares ? applyMiddleware(...middlewares) : null; | ||
const store = createStore(rootReducer, window.__PRELOADED_STATE__, appliedMiddlewares); | ||
|
||
delete window.__PRELOADED_STATE__; | ||
|
||
ReactRouterSSR = () => ( | ||
<Provider store={store}> | ||
<BrowserRouter> | ||
{component} | ||
</BrowserRouter> | ||
</Provider> | ||
); | ||
} | ||
|
||
FastRender.onPageLoad(() => { | ||
ReactDOM.hydrate(<ReactRouterSSR />, document.getElementById(renderTarget)); | ||
}); | ||
FastRender.onPageLoad(() => { | ||
ReactDOM.hydrateRoot(document.getElementById(renderTarget), AppJSX); | ||
}); | ||
}; | ||
|
||
|
||
export { renderWithSSR }; |
Oops, something went wrong.