Skip to content

React 18 root support - #1286

Draft
LinusCenterstrom wants to merge 2 commits into
reactjs:mainfrom
MultinetInteractive:react-root-support
Draft

LinusCenterstrom wants to merge 2 commits into
reactjs:mainfrom
MultinetInteractive:react-root-support

Conversation

@LinusCenterstrom

@LinusCenterstrom LinusCenterstrom commented Apr 1, 2022 •

Copy link
Copy Markdown

fixes #1285
This is mostly unrelated to #1265 and still uses the renderToString API

Things that should probably be done before this is merged (happy to help with this if you want)

  1. Update the documentation.
  2. Update the code for ReactRouter to make this work there as well.

I have not looked into making this work with the automatically bundled versions of React & ReactDOM if LoadReact is set to true.

For anyone else looking to make this work:
To make the js code run server-side you have to change the import of react-dom/server to instead be
eact-dom-server-legacy.browser.production.min.js or react-dom/cjs/react-dom-server-legacy.browser.development since the new server-renderer uses classes
that do not exist in the .net JS environment and instantly crashes.

The globally exposed window.ReactDOM object on the clientside should be imported from react-dom/client instead of react-dom

@cb-eli

cb-eli commented Apr 10, 2022

Copy link
Copy Markdown

@LinusCenterstrom, In your PR, is it possible to control the used API per root component?
Or, it's a global configuration?

@LinusCenterstrom

Copy link
Copy Markdown
Author

It's currently only a global configuration

@dustinsoftware

dustinsoftware commented Apr 11, 2022 via email

Copy link
Copy Markdown
Contributor

@LinusCenterstrom

Copy link
Copy Markdown
Author

Thanks!!

I see the PR checks are failing, is that new to this PR?

Don't think so, looks like they are failing on all PR:s from what I can see

@npossamai npossamai left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm interested on react 18 support too!
nice job @LinusCenterstrom, thanks!

any update on the pull request status?

@magnusottosson

Copy link
Copy Markdown

I also need this. Is the project dead? Any alternatives to get React 18 support?

@LinusCenterstrom

Copy link
Copy Markdown
Author

@magnusottosson If you are still looking, you could use the main branch of our fork here https://github.com/MultinetInteractive/React.NET which I made to fix it for my company's use. It is only supported if you supply your own version of React & ReactDOMServer. (.SetLoadReact(false) during startup)

The initialization code for our serverbundle looks like this (we bundle through webpack)
(Make sure the relative paths for the imports are correct if you copy paste)

var ReactDOMServer = require(process.env.NODE_ENV !== "production"
	? "../node_modules/react-dom/cjs/react-dom-server-legacy.browser.development"
	: "../node_modules/react-dom/cjs/react-dom-server-legacy.browser.production.min.js");

var React = require("react");
var ReactDOM = require("react-dom");

if (!global) {
	global = {};
}

global.React = React;
global.ReactDOM = ReactDOM;
global.ReactDOMServer = ReactDOMServer;

We've been using it in production pretty much since I opened this PR and it seems to work fine. It is not very likely that we will add support for anything other than what we use internally however.

@va-timorris

Copy link
Copy Markdown

It feels like the repo is dead - nobody managing it. What a shame.

@rshackleton

Copy link
Copy Markdown

@va-timorris check out https://github.com/DaniilSokolyuk/NodeReact.NET

/// Enables usage of the React 18 root API when rendering / hydrating.
/// </summary>
/// <returns></returns>
void EnableReact18RootAPI();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void EnableReact18RootAPI();
IReactSiteConfiguration EnableReact18RootAPI();

Comment on lines +389 to +392
public void EnableReact18RootAPI()
{
UseRootAPI = true;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void EnableReact18RootAPI()
{
UseRootAPI = true;
}
public IReactSiteConfiguration EnableReact18RootAPI()
{
UseRootAPI = true;
return this;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support the new React 18 root API

7 participants