question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using react with ES5

See original GitHub issue

Firstly 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:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
developitcommented, Dec 6, 2016

Thanks @robertknight, you took the words right out of my mouth! 😃

This was my initial thought when seeing your code sample:

function Comp() {
  this.doThings = function () {
    this.setState(true);
  }.bind(this);
}
Comp.prototype = new preact.Component;
Comp.prototype.render = function (props, state) {
  return preact.h('a', {href:props.href, onclick: doThings}, state ? 'before': 'after');
};

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():

// helper for building ES5 components
function createClass(obj) {
  function C() {
    preact.Component.apply(this, arguments);
    for (var i in obj) if (i!=='render' && typeof obj[i]==='function') this[i] = obj[i].bind(this);
    if (obj.init) obj.init.call(this);
  }
  C.prototype = new preact.Component;
  for (var i in obj) C.prototype[i] = obj[i];
  C.prototype.constructor = C;
  return C;
}


// Usage Example:
createClass({
  init: function() {
    // init is basically your constructor
    this.state = { thing: false };
  },
  doThings: function() {
    // all methods are bound to the class instance automatically
    this.setState({ thing: true });
  },
  render: function(props, state) {
    return preact.h('a', {href:props.href, onclick: this.doThings}, state.thing ? 'before': 'after');
  }
});

@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 👍

3reactions
robertknightcommented, Dec 6, 2016

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:

function Label() {
	this.render = function () {
		return preact.h('div',{},'Hello World');
	}
}
Label.prototype = Object.create(preact.Component.prototype);
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found