-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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.createElement #168
Comments
@ngasull - hi! preact-compat exports a nearly identical interface to React & ReactDOM (combined) in order to meet the use-cases you described. It exports not only Does that work for your setup? |
@developit: acutally, my setup is about switching between react and preact without changing the code, and without
|
Hmm - mind expanding on the react-hot-loader issue? This is the first I've heard about it, seems like an issue worth solving regardless :) As for the exports, it would be somewhat cheap yes, though as if yet preact has never had to duplicate things in order to emulate react. That role has intentionally been delegated to Another option though, while I'm thinking on it, would be to use a Babel or Webpack environment configuration value to switch the JSX Pragma between |
@ngasull By the way, for // src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
function init() {
let App = require('./components/app').default;
ReactDOM.render(<App />, document.body);
}
// always render once:
init();
// add hot module replacement:
if (module.hot) module.hot.accept('./components/app', init); Just the last line is how you set up HMR for a whole project. Updates for all other components are accepted because they are dependencies of App (or whatever your root is). |
@developit when setting up As for your suggestion of configuring As for enzyme, I'll come back when I have more info on why it breaks. Thanks for considering this issue! |
@developit so I figured out some things wrong with enzyme using preact-compat:
Do you have any better suggestion for testing in preact? Enzyme enables shallow rendering which would be really great to have in preact as well. |
@ngasull Think it'd be kosher to just export an empty string? That'd at least let Enzyme call For my own testing, I use JSX assertions (via preact-jsx-chai) for testing render() and pure functions and shallow rendering. For cases where I need to observe the DOM and react to it, I just use Preact's normal // shallow (no dom)
expect(<Foo />).to.equal(<Bar from="foo" />);
// deep (no dom)
expect(<Foo />).to.deep.equal(<div class="bar">bar</div>);
// dom example
let c, scratch = document.createElement('div');
render(<Foo ref={c => foo=c } />, scratch);
expect(c.state).to.eql({ foo: 'bar' });
c.handleFooOrSomething('baz');
expect(c.state).to.eql({ foo: 'baz' }); |
@developit any string would work as a version. I used "preact" locally and enzyme just fell back on its default, thanks for considering this change. I didn't know about preact-jsx-chai. Really interesting stuff, I'll give it a try. However I'd love to have partial attributes comparison in my tests! |
In relation to this. I tried to set up HMR using native webpack 2 features - i.e. no TypeError: Cannot redefine property: type
at Function.defineProperty (native)
at :/~/preact-compat/dist/preact-compat.js:39:1
at PropTypes (:/~/preact-compat/dist/preact-compat.js:2:1)
at Object.module.exports.exports.__esModule (:/~/preact-compat/dist/preact-compat.js:5:2) Here is the code in question: Object.defineProperty(VNode.prototype, 'type', {
get: function () {
return this.nodeName;
},
set: function (v) {
this.nodeName = v;
}
}); Probs gets executed on the first run, therefore fails on second. Would a quick prop check suffice? I'm happy to PR if you feel so. |
@ctrlplusb ah nice catch - yeah a check or even just throwing PR would be awesome! Anything patching stuff into Preact should really be setting |
Sweet, on it. :-) |
I have created a simple example of how to use native HMR of webpack 2 with preact/preact-compat. |
Hey guys, I'm trying to get my head around this, but not having much luck. I tried the following but keep getting the Can someone point me in the right direction? Thanks Command line:
|
@nishtacular it seems like you might have the wrong file path being passed to |
Here's a thought (relating to the original question): what if import preact from 'preact/compat';
preact; // the normal export, but decorated with low-hanging fruit compat fixes:
preact.createElement // alias of h() If we were to do this, are there any other trivially simple aliases we could add? |
@ngasull Can you answer please cause we are following the discussion? :) |
@developit I really like the idea of isolating "compat aliases" into a module! 😄 So far I didn't need anything else than |
Sweet. If anyone wants to do a PR I'm open to it, or I can try to get something out tonight. |
@developit I'd be glad to do the PR 👋 just checking about the naming: importing from |
Agreed, that name would be a bit chaotic. |
I'm just not sure on how to expose this submodule: today, "main" module targets
|
might have to transpile to If we're cool with saying only ES Modules are supported for |
One last suggestion with transpiling: Adding files to the root just doesn't feel clean 😅 |
I agree that root files isn't the nicest, though it's pretty easy to transpile to the root and only have them deployed in the npm package. Are we over-complicating it by doing it in the preact module instead of just doing |
Ok for transpiling to the root then! I prefer having aliases in If agreed, I'll take care of the PR! |
I think we can finally close this since |
I want to replace my code from alias: {
'react': require.resolve('preact-compat'),
'react-dom': require.resolve('preact-compat'),
} Because I use |
@windyGex there is an open issue in preact-compat for enzyme compatibility: preactjs/preact-compat#82 |
ok, thx, I will continue to focus on this. |
Fix vnode ordering issue when devtools are open
Hi there! Question for @developit :
I'm considering to use preact because 3kb for the React API is just amazing. However, react's ecosystem has a lot more tools at the moment especially
react-hot-loader
andenzyme
for testing, which are not compatible with preact or preact-compat.In order to benefit from both of both worlds, I'm importing
react
andreact-dom
in the code, use original react in development, and use an alias onpreact
when really making the bundle. This way I realized thatcreateElement
was simply not in preact, thus making it not a drop-in replacement of react. I think this is really unfortunate, in my case, and as this could have encouraged people taking advantage of preact.Don't you think exporting
createElement
as an alias ofh
would be good?I considered the
.babelrc
jsx pragma solution but this is really inconvenient as I'd like to manage choosing react or preact from my webpack configs.NB: if you have better options to suggest me in terms of
enzyme
/hot-loader
tooling I'm definitely interested 😃The text was updated successfully, but these errors were encountered: