Does a React application HAVE to run on its own server process? - reactjs

I come from a background in Angular, but I wanted to start learning React. I want to make a React front-end for a nodejs Express web service.
In Angular, I could simply build the source code into a static folder with a standard index.html, and it can be deployed on any web server. With React however, every tutorial I see is about running React in its own server using create-react-app and npm start.
I know you can also just use script tags to import the React framework in a static page, but then you can't use JSX syntax, and I would like to. Is it possible to simply build a React project into a static folder with an index.html that can be deployed on any web server?

Yep, you can do what you're describing. If you want to use JSX syntax, you'll need Babel, which transpiles it into standard JavaScript.
You can avoid the complexities of setting it up by using create-react-app to create the app, then running npm build instead of npm start. This will compile everything into a build directory, complete with index.html.

CRA uses its server for development purposes. You don't need CRA for using React of course. But, as your app getting bigger and bigger you will need some more extra tools. For example you want to see your code instantly without refreshing your browser. Here comes the dev server and hot reloading.
CRA uses Webpack behind the scenes. It is a great tool for bundling (obviously) all your files (including css), minifying, uglifying, optimizing your code etc.
For simple code or testing purposes using one file and attaching React and Babel to your file could be enough but for real apps you will need more. Either you will use something like Webpack on your own or you will use CRA and let it do all the extra stuff for you.

Related

How can i run reactjs locally on mac other than the npx create-react-app?

I'm just a bit annoyed by having to do npx create-react-app and wait then delete most of the files and code.
I've seen some people use npm i react react-dom, is this another way as well?
can i also use react offline ?
You can add a in your html if you don't want to use create-react-app. Then just create a file with that name. Otherwise I believe other methods still give you a boilerplate.
Even if without create-react-app, can make React applications. But it's bother than with it. create-react-app just automates downloading libraries and many many configurations. So what they do is same.
Create react app creates spa and gives entire folder structure out of the box, but you don't always need to use cra command for that, you can create spa yourself with minimal packages if you know the actual working of spa, you can use react, webpack, webpack dev server, babel.

excluding a library during bundle

I am new to npm, react and webpack but I have a question. In npm how do you prevent a library from being included production package file?
For example, I am just building a very small component in react and on the site where I am going to place my small component. The problem is Jquery library and bootstrap is already being called in the masterpage of the site and I didn't want to call the same library again in my small application production build but I still want to use them during development(because I am only testing it on local and jquery is not called there). TIA
Appreciate your time ready this and hope I got to learn more from you. By the way I created the app using create-react-app but I already 'ejected' it, so its using the webpack 3
Take a look at Webpack externals
You can have two webpack configs, on the dev config you include the package as normal, but for your production config add externals for jquery, so it uses the global one already on the page
The ability you're looking for is named Code splitting
React loadable may be a good option for you as well

Edit Files After Build, React

I built an app in React with create-react-app. Just JavaScript, CSS, HTML & React. I ran npm build then deployed the app to Netlify.
I want to go back and edit some CSS. So, I cd into the directory from my laptop and deploy on localhost:5000. I open VS Code and make changes however none of the changes are reflected in the browser # localhost:5000.
When I was building the app, the way I had it set up allowed me to view each change immediately in the browser when I save the file.
Are files editable after you run npm build? What am I missing here?
When you run a build on a react app (or any other app) code will be converted from es6 to es5 and then probably minified (depends on webpack config) so code is unreachable and you need .map files to debug code in production environment.
So the most clean way to act on deployed code is to make a new build with updated features and deploy again the frontend.
In local development react boilerplates usually make intensive use of hot-reload, a plugin that allow code to be hot replaced while app is running.
Built application instead load chunks of JS files once and CACHE it. So in order to see your changes you have to clean cache or force a refresh (home+F5 on windows, CMD+R on OSX) to be sure that your changes are visible.
Despite this I discourage to edit the build files. When you have to update the code stay on development mode, before deploy, build your code and test it live.
You could create some files outside the src folder and access them with fecth from app.js or even import them from index.html ... so if you wanted to change something you could do it without having to do a build again.

Can you and How to serve react js or angular js without a static sever?

How to use the production version of a react or angular project and run without a local server?
( like opening from index.html or something similar)
I want to make a static web application that uses react or angular 2 as a starting point. Eventually, this will be dynamic so it make sense to take advantage of either.
I don't mind using local http-serve to serve the html and css pages in development but due to some restrictions i won't be able to set up a local server or run npm start from the terminal.
I did a lot of research but never found a definitive answer as to
So like How to use the production version of a react or angular project and run without a local server?
( like opening from index.html or something similar
React (same as Angular) is a client-side library/framework. So all you have to do is to bundle your application to a single .js file. That file will probably include React/Angular and you may just load it in your HTML file. You definitely don't need a http-serve for that. It is possible to deploy your index.html and your bundle.js file to a shared hosting and just load the app.

Production Server for React Redux Starter Kit

I am using the React Redux Starter Kit from DaveZuko, and am stuck on how to create production server to serve the compile client side code.Ideally I would just like to use the Koa server and move it to production, but can't find out how to do that.
All relevant code is unchanged from the repository here: https://github.com/davezuko/react-redux-starter-kit
Does anybody know how to compile a product server for this?
I did try to use davezuko's starter kit, but it was really hard to follow what those configuration files really did and messed them up when trying to install packages that were missing.
I also tried facebook's create-react-app, but that was really complex too when you run the eject command, so I gave up and created my own starter kit http://redux-minimal.js.org/ . I has the minimum amount of packages that lets you build rich real-word apps, but doesn't have the cluttered configuration boilerplate that other starter kits have.
Now, answering your question, with redux minimal, you just do "npm run build-prod" and it compiles the css and js files for the production environment, minified and ready to go. Then you just copy the public folder which contains the html file too, paste to what ever server you want, and then call the index.html url and it works.

Resources