Using react with ES5
See original GitHub issueFirstly id like to say thanks for creating a really nice library. Ive been a fan of the react style approach and im really happy you took the time to trim the fat of react and produce preact.
Im currently using react to great success in my project, and for the most part its stateless functions. However I’ve found a need recently for a custom component and would like to create one without needing to introduce an ES6 transformation step into my build system.
Ive tried creating components in ES5 with plain old prototypical inheritance but don’t seem to be getting anywhere fast, i don’t suppose someone has tackled this problem already ?
Cheers
C
Mini example i tried
var Comp = function () {
this.doThings = function () {
this.setState(true);
}.bind(this);
this.render = function (props, state) {
return preact.h('a', {href:props.href, onclick: doThings}, state ? 'before': 'after');
}.bind(this);
}
Comp.prototype = preact.Component;
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
React Without ES6
Autobinding. In React components declared as ES6 classes, methods follow the same semantics as regular ES6 classes. This means that they don't automatically ......
Read more >How ReactJS ES6 syntax is different compared to ES5
How ReactJS ES6 syntax is different compared to ES5 ? · ES5 uses the createReactClass() or React. · Object manipulation is slow for...
Read more >React cheatsheet: ES5 vs ES6 - remarkablemark - Medium
The differences between ES5 and ES6 syntax when writing a React application.. “React cheatsheet: ES5 vs ES6” is published by remarkablemark.
Read more >Comparing React With ES5 vs. React With ES6 - DZone
The ES5 and ES6 syntaxes are pretty similar, and if you haven't done any JavaScript for a while (or at all), it can...
Read more >Creating & Using Functions in React: ES5 vs ES6
The .bind is usually used in tandem with ES5 functions as a way of preserving the function's context. Since ES6 functions preserve the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks @robertknight, you took the words right out of my mouth! 😃
This was my initial thought when seeing your code sample:
If you’re wanting to avoid transpilation, it’s possible you could just inline preact-classless-component. It’s really small and would avoid you having to
.bind()
and do inheritance just to get.setState()
:@chrismatheson - let me know what you end up going with, I’d love to set up some better examples for Preact + ES3 (no transpiler). We have this and this, but the former could probably show off some helpers like the one above.
Or maybe something cool and new - who knows, maybe there is a great new functional way to create components out there I’ve not seen yet 👍
Preact checks whether an object is a stateful component by looking for a
render
function on the object’s prototype.In other words,
<Your Component>.prototype.render
needs to return the render function.You can use ES5, but you need to understand what the equivalent to
class MyComponent extends preact.Component
is: