expressjs / generator

Express' application generator
MIT License
1.82k stars 537 forks source link

React support #251

Closed boardfish closed 4 years ago

boardfish commented 4 years ago

This was previously explored through this PR. React has official support for Express through this project, which seems to be well-maintained. Given the discussion that happened on the PR, it might be more work than I'd expect, but it'd be good if there was a react or jsx option for views as suggested previously.

Until this happens, React can be used as a backend manually like this.

ionutale commented 4 years ago

I think a flag -api is much better This eliminates the front view boilerplate. And then you create your FE with whatever you want. Microservice fashion

This way with 2 comands you have both worlds express microservice-api npx create-react-app awesome-project-name

ryhinchey commented 4 years ago

. Given the discussion that happened on the PR, it might be more work than I'd expect, but it'd be good if there was a react or jsx option for views as suggested previously.

It'd definitely be cool to have jsx support in the generator. That said, it sounds like there was an issue with express-react-views on versions node 4 and below. Express supports pretty low versions of node and that won't change until the next major (v5) release.

It looks like express-react-views tests on node v6 and up per their travis.yml

I'm sure there would be interest in a new PR building off the work & comments here https://github.com/expressjs/generator/pull/187 and knowing it'd probably have to wait to land until the next major version of express.

dougwilson commented 4 years ago

We don't have to add only view engines that work with all Node.js versions (though that would be ideal)--we could either have the option error out when used on old Node.js versions or (better, if possible) default to older jsx versions that have that support when run on the older versions.

ryhinchey commented 4 years ago

It looks like express-react-views supports React down to 0.14.0 and 0.14.0 of react supports node 4 and above.

we could either have the option error out when used on old Node.js versions or (better, if possible) default to older jsx versions that have that support when run on the older versions.

yah - we could error out on node < 4 and install the latest versions of react and react-dom that work on the user's node version. That said, if you try to use a newer React feature that's included in a version of React which the express generator didn't install (because of your node version), your app won't render.

There are several options for doing static rendering with React components (a lot of them like Gatsby use express under the hood). There are also plenty of examples of how to set up server side rendering and react with express.

If the generator implements React/JSX as a view engine (generates static html without bundling client side javascript), do you think people would also expect it to work like gatsby and next.js (that do server side rendering and bundle your JS, etc)?

boardfish commented 4 years ago

I expected express-react-views to work that way on first use. I don't think it's a huge problem that it doesn't, but as with version mismatches and other issues discussed, it's something that should be flagged up at the console, I reckon.

My original reason for asking for this has been put to bed (we're actually forced to use jQuery for this assignment I'm working on :disappointed:) but I'll gladly follow this issue nonetheless. Hopefully there'll be demand for it from elsewhere.

ryhinchey commented 4 years ago

I expected express-react-views to work that way on first use.

yah - that's my concern with adding something like jsx to the generator. People will likely assume it bundles your JS, does server side rendering and adds the necessary code to boot up your app on the client which IMO is beyond the scope of what this generator should do

ryhinchey commented 4 years ago

I'm going to go ahead and close this issue. There are solid options for using react purely as a templating language with express (express-react-views) and other great options for doing server side rendering with react.