Skip to content

Commit 6db5286

Browse files
committed
react hot loader without ejecting
1 parent 9121717 commit 6db5286

10 files changed

+138
-74
lines changed

Node-Express/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ This means that Node.js is a program we can use to execute JavaScript on our com
1616
This engine takes your JavaScript code and converts it into a faster machine code. Machine code is low level code which the computer can run without needing to first interpret it.
1717
Firefox uses Mozilla’s SpiderMonkey JS engine, and Microsoft products use its Chakra engine.
1818

19-
The Node.js Execution model
19+
### The Node.js Execution model
2020
In very simplistic terms, when you connect to a traditional server, such as Apache, it will spawn a new thread to handle the request. In a language such as PHP or Ruby, any subsequent I/O operations (for example, interacting with a database) block the execution of your code until the operation has completed. That is, the server has to wait for the database lookup to complete before it can move on to processing the result. If new requests come in while this is happening, the server will spawn new threads to deal with them. This is potentially inefficient, as a large number of threads can cause a system to become sluggish — and, in the worse case, for the site to go down. The most common way to support more connections is to add more servers.
2121

22-
Node.js, however, is single-threaded. It is also event-driven, which means that everything that happens in Node is in reaction to an event. For example, when a new request comes in (one kind of event) the server will start processing it. If it then encounters a blocking I/O operation, instead of waiting for this to complete, it will register a callback before continuing to process the next event. When the I/O operation has finished (another kind of event), the server will execute the callback and continue working on the original request. Under the hood, Node uses the libuv library to implement this asynchronous (i.e. non-blocking) behavior.
22+
Node.js, however, is single-threaded. It is also event-driven, which means that everything that happens in Node is in reaction to an event.
23+
24+
### For example, when a new request comes in (one kind of event) the server will start processing it. If it then encounters a blocking I/O operation, instead of waiting for this to complete, it will register a callback before continuing to process the next event. When the I/O operation has finished (another kind of event), the server will execute the callback and continue working on the original request. Under the hood, Node uses the libuv library to implement this asynchronous (i.e. non-blocking) behavior.
2325

2426
Node’s execution model causes the server very little overhead, and consequently it’s capable of handling a large number of simultaneous connections. The traditional approach to scaling a Node app is to clone it and have the cloned instances share the workload. Node.js even has a built-in module to help you implement a cloning strategy on a single server.
2527

React/Redirect-from-react-router-dom.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
1> Official Doc - https://reacttraining.com/react-router/web/api/Redirect
66

7-
Rendering a <Redirect> will navigate to a new location. The new location will override the current location in the history stack, like server-side redirects (HTTP 3xx) do.
7+
Rendering a <Redirect> will navigate to a new location. The new location will override the current location in the history stack, like server-side redirects (HTTP 3xx) do.
88

99
2> https://medium.com/@anneeb/redirecting-in-react-4de5e517354a
1010

React/hot-reloading-in-React.md

-68
This file was deleted.

React/react-hot-loader.md

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
### React Hot Loader is a plugin that allows React components to be live reloaded without the loss of state. It works with Webpack and other bundlers that support both Hot Module Replacement (HMR) and Babel plugins. (see my note in file webpack-Hot-Module-Replacement.md) It is the replacement of code in a running application without requiring a restart or reloading of the application. It is very useful when developing because you can see your changes as you make them thus giving immediate feedback. React hot loading depends on a working Webpack HMR setup
2+
3+
https://gaearon.github.io/react-hot-loader/getstarted/ - This is a good resource to implement HMR (Hot Module Replacement) in React
4+
5+
## Hot reloading In create-react-app AFTER ejecting
6+
7+
/home/paul/codes-Lap/React/boilerplate/redux-boilerplate-base-counter-AFTER-EJECTING
8+
9+
### First configuration in webpackDevServer.config.js
10+
11+
// Enable hot reloading server. It will provide /sockjs-node/ endpoint
12+
// for the WebpackDevServer client so it can learn when the files were
13+
// updated. The WebpackDevServer client is included as an entry point
14+
// in the Webpack development configuration. Note that only changes
15+
// to CSS are currently hot reloaded. JS changes will refresh the browser.
16+
``hot: true,``
17+
18+
# How React Hot Loader works
19+
20+
https://github.com/gaearon/react-hot-loader.wiki.git
21+
22+
React-Hot-Loader was created to solve 2 tasks:
23+
24+
- Seamlessly replace the old Components, by the new ones.
25+
- Hide all updates from React, to not let him remount changed branches, and thus drop the information stored in component instances, e.g state.
26+
27+
To achieve these goals React-Hot-Loader
28+
29+
- first detects which "old" components shall be replaced by which new ones.
30+
- swaps the real code "behind" the components.
31+
32+
Hot module replacement, enchanted with React-hot-loader, might look like a awesome concept, helping you to build application faster, and better, but, according to some, it also has some drawback like A) Compilation is not so “hot”. It may took a minute to build a bundle or B) It empowers monkey-patching, not the tests first approach.
33+
34+
# How to turn on Hot Module Replacement in create-react-app without ejecting
35+
36+
### While implementing the react-hot-loader without ejecting create-react-app I have followed []this guide](https://daveceddia.com/hot-reloading-create-react-app/)
37+
38+
And my [live app here-a boilterplate basic counter with react and redux](https://github.com/rohan-paul/redux-boilerplate-base-counter/tree/master/redux-boilerplate-base-counter-without-ejecting)
39+
40+
https://daveceddia.com/hot-reloading-create-react-app/
41+
42+
#### Create React App don’t have Hot Module Replacement (HMR) set up by default. HMR allows us to replace modules in-place without restarting the server.
43+
44+
Install the following 3 packages
45+
46+
``yarn add react-app-rewired react-app-rewire-hot-loader react-hot-loader``
47+
48+
Create a file called config-overrides.js in the root directory of your project (not under “src”) with this code:
49+
50+
```js
51+
const rewireReactHotLoader = require('react-app-rewire-hot-loader');
52+
53+
module.exports = function override(config, env) {
54+
config = rewireReactHotLoader(config, env);
55+
return config;
56+
}
57+
58+
```
59+
Change index.js accordingly:
60+
61+
```js
62+
import React from 'react';
63+
import ReactDOM from 'react-dom';
64+
import './index.css';
65+
import App from './App';
66+
import registerServiceWorker from './registerServiceWorker';
67+
68+
// Add this import:
69+
import { AppContainer } from 'react-hot-loader';
70+
71+
// Wrap the rendering in a function:
72+
const render = Component => {
73+
ReactDOM.render(
74+
// Wrap App inside AppContainer
75+
<AppContainer>
76+
<App />
77+
</AppContainer>,
78+
document.getElementById('root')
79+
);
80+
};
81+
82+
// Do this once
83+
registerServiceWorker();
84+
85+
// Render once
86+
render(App);
87+
88+
// Webpack Hot Module Replacement API
89+
if (module.hot) {
90+
module.hot.accept('./App', () => {
91+
render(App);
92+
});
93+
}
94+
```
95+
96+
### Change package.json to use react-app-rewired instead of react-scripts. In the “scripts” section, change this:
97+
98+
```js
99+
"scripts": {
100+
"start": "react-scripts start",
101+
"build": "react-scripts build",
102+
"test": "react-scripts test --env=jsdom"
103+
}
104+
```
105+
106+
To this:
107+
108+
```js
109+
"scripts": {
110+
"start": "react-app-rewired start",
111+
"build": "react-app-rewired build",
112+
"test": "react-app-rewired test --env=jsdom"
113+
}
114+
115+
```
116+
117+
### What is repalaceReducer()
118+
119+
Replaces the reducer currently used by the store to calculate the state. You might need this if your app implements code splitting, and you want to load some of the reducers dynamically. You might also need this if you implement a hot reloading mechanism for Redux.
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
### Official Guide - https://webpack.js.org/guides/hot-module-replacement/
2+
3+
Hot Module Replacement (or HMR) is one of the most useful features offered by webpack. It allows all kinds of modules to be updated at runtime without the need for a full refresh.
4+
5+
### Official concept - https://webpack.js.org/concepts/hot-module-replacement/
6+
7+
### Hot Module Replacement (HMR) exchanges, adds, or removes modules while an application is running, without a full reload. This can significantly speed up development in a few ways:
8+
9+
Retain application state which is lost during a full reload.
10+
Save valuable development time by only updating what's changed.
11+
Tweak styling faster -- almost comparable to changing styles in the browser's debugger.

Redux/dispatch.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ I am using dispatch() to send the type along with the data that we get from the
5757

5858
This double function strategy allows us to wait for an asynchronous operation (like fetching data) to complete, and then the action is returned by the thunk.
5959

60-
The plain data flows in a typical Redux dispatch(action) -> reducer -> new state -> re-render
60+
The plain data flows in a typical Redux is like this >> dispatch(action) -> reducer -> new state -> re-render
6161

6262
The adjusted order, including reducers, is: dispatch ➡️ action creator ➡️ thunk ➡️ action ➡️ reducer.

Redux/redux-logger-basics.md

Whitespace-only changes.

Redux/redux-thunk-basics.md

Whitespace-only changes.

webpack/webpack.config-file.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ While creating index.html manually works good, it would be nice if webpack can c
2323
Webpack can do this for us with the help of html-webpack-plugin. Using this plugin has some added advantages like auto-hashing the ‘src’ attribute of the embedded <script> tag every time the webpack is run, which makes browser to get the latest version of the file from server instead of using a cached one whenever it has a new hash.
2424

2525

26-
In **create-react-app** - this is module is used extensively, which I can see after ejecting
26+
In **create-react-app** - this module is used extensively, which I can see after ejecting
2727
The below from ``./config/webpack.config.prod.js``
2828
```js
2929
// Generates an `index.html` file with the <script> injected.
@@ -61,7 +61,7 @@ And the below from ``./config/webpack.config.dev.js``
6161

6262
# Loaders
6363

64-
Loaders let you run preprocessors on files as they’re imported. This allows you to bundle static resources beyond JavaScript, but let’s look at what can be done when loading .js modules first.
64+
Webpack accepts a loader object which specify loader to apply to files that match the test regex and exclude files that match the exclude regex. So, loaders let you run preprocessors on files as they’re imported. This allows you to bundle static resources beyond JavaScript, but let’s look at what can be done when loading .js modules first. In this below case we’re applying the babel-loader to all files with a .js extension that aren’t in node_modules and are not in bower_components
6565

6666
module: {
6767
+ rules: [

0 commit comments

Comments
 (0)