Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React 18 root support #1286

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

LinusCenterstrom
Copy link

@LinusCenterstrom LinusCenterstrom commented Apr 1, 2022

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
Copy link

cb-eli commented Apr 10, 2022

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

@LinusCenterstrom
Copy link
Author

It's currently only a global configuration

@dustinsoftware
Copy link
Member

dustinsoftware commented Apr 11, 2022 via email

@LinusCenterstrom
Copy link
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

Copy link

@npossamai npossamai left a comment

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

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

@LinusCenterstrom
Copy link
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

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

@rshackleton
Copy link

@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();

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;
}

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