Comments (9)
@gaearon why don't replace React.creactClass
with hmr proxy on top of the app? RegExp check is a mess. Even if replace it with a parser like esprima or graspjs the variable name is unpredictable.
from react-hot-loader.
I think it is possible to inject hmr code once in app entry point instead of patching every module. If you don't have time for this I could try to manage the issue and send a PR.
from react-hot-loader.
You can't inject it on top because each module needs its own module-specific createClass
function.
You can probably do following:
- Instead of replacing regex, monkey-patch
React.createClass
at the beginning of each module - Restore
React.createClass
at the end of each module (otherwise first require in the file will mess it up) - Currently we opt out of hot reload for modules that don't have regex matches. If we say goodbye to regex, we must only enable hot reload (
if module.hot
code) if monkey-patchedcreateClass
was called during module definition.
from react-hot-loader.
Confused about point 3. Why need this check if patched createClass
will be called explicitly every time we want create a component?
from react-hot-loader.
Modules opt in to HMR by calling module.hot.accept
. We don't want to opt-in to HMR for files that contain no components. For plain JS files, it's best to let HMR bubble up dependency tree because for most React apps, there is a component somewhere on the too, and it is HMR-capable by itself. As a consequence, side-effect free plain JS modules will get hot reloading as a bonus by the virtue of being require from HMR-capable components.
However, if we just put module.hot.accept
in every file (as opposed to those containing createClass
calls), Webpack will think that these files are truly HMR-capable (which they are not), and won't bubble reload up when they change.
Right now we add module.hot.accept
when regex matches, but if we do away with regex, we must have other way to know if module is “componentish” or not. One way to do that is to check if monkeypatched createClass
was called by the end of module definition. If not, probably this module contains no components, and therefore we shouldn't emit module.hot.accept
, or we lose the benefits of HMR bubbling.
Finally, this is only a problem if regular (non-component) files go through this loader. This is often the case. Personally, I use JSX transforms even on plain JS files so I can use Harmony syntax and some JSX occasionally in helpers.
from react-hot-loader.
1.0.0-alpha
no longer uses the regex. Instead, only module.exports
is hot-enabled implicitly.
However, there is an API to hot-enable any class (e.g. a private one, or one inside a function):
var SomeComponent = React.createClass({ ... }); // or ES6 class in the future
if (module.makeHot) {
SomeComponent = module.makeHot(SomeComponent);
}
from react-hot-loader.
Great, looks much better now. Will you drop components store by displayName?
from react-hot-loader.
Yeah, there's no need for that now.
Now it works like this:
module.exports
is always stored implicitly, regardless ofdisplayName
- call to
module.makeHot(SomeComponent)
by default usesdisplayName
orname
(ES6), but you can specify any other key as a second parameter
from react-hot-loader.
Closing, as this will be available in 1.0.
from react-hot-loader.
Related Issues (20)
- React.useEffect's callback will be called when the page is updated HOT 2
- Hot reloading tested in different ways and is not working properly HOT 1
- Sometimes hot() is needed not only for the root component HOT 2
- Hot-Reload fails with “Unexpected token '['. Expected opening '{' at the start of a class body.” HOT 1
- Warning: The final argument passed to useEffect changed size between renders.
- Could not resolve dependency HOT 12
- Not working with react-router-config: Uncaught ReferenceError: module is not defined HOT 9
- Reload Full Page instead of Refresh? HOT 1
- Attempted import error: 'AppContainer' is not exported from 'react-hot-loader'. HOT 1
- document click listener function executed twice: before and AFTER component rendered. HOT 4
- when props include "this"
- How to deal with multiple entries
- in sub project cannot replace in time
- React-hot-loader compatibility with React 16.14.0 HOT 1
- React 18 upgrade HOT 5
- Yarn 3 PNP not compatible
- Regular Expression Denial of Service (ReDoS) HOT 3
- hmr no work in React.PureComponent HOT 2
- Analysis: 98% of dependency updates in this repository can be merged.
- React-Hot-Loader: misconfiguration detected, using production version in non-production environment.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-hot-loader.