{"id":3405,"date":"2016-03-16T16:52:49","date_gmt":"2016-03-16T16:52:49","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3405"},"modified":"2016-03-16T16:52:49","modified_gmt":"2016-03-16T16:52:49","slug":"converting-javascript-objects-es6-classes","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/converting-javascript-objects-es6-classes\/","title":{"rendered":"Converting Javascript objects to ES6 classes"},"content":{"rendered":"<p>If you are upgrading React, you may find situations where you want to switch from Javascript objects to ES6 classes.<\/p>\n<p>For instance, say you have these objects:<\/p>\n<pre lang=\"javascript\">\nvar MyMixin = {\n  componentShouldUpdate: function() {\n    \/\/ some logic\n  }\n}\n\nReact.createClass({\n  mixins: [MyMixin]\n  getInitialState: function() {\n    \/\/ do some stuff...\n  },\n  render: function() { \n    return (<div><\/div>)\n  }\n})\n<\/pre>\n<p>Ideally you would convert everything to classes, and use inheritance to get the mixin in:<\/p>\n<pre lang=\"javascript\">\nclass MyMixin {\n  function componentShouldUpdate() {\n    \/\/ some logic\n  }\n}\n\nclass MyComponent {\n  constructor() {\n    \/\/ do some stuff...\n    this.state = {}\n  }\n\n  render() { \n    return (<div><\/div>)\n  }\n})\n<\/pre>\n<p>If you can&#8217;t do this (e.g. the mixin is in a library, or it&#8217;s too much code to change at once), you can also monkey-patch the mixin&#8217;s methods into a base class for the component:<\/p>\n<pre lang=\"javascript\">\nvar MyMixin = {\n  componentShouldUpdate: function() {\n    \/\/ some logic\n  }\n}\n\nclass MyComponent extends (\n  React.createClass({ \n    mixins: [MyMixin],\n    render: () => {}\n  }) {\n  constructor() {\n    \/\/ do some stuff...\n    this.state = {}\n  }\n\n  render() { \n    return (<div><\/div>)\n  }\n})\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you are upgrading React, you may find situations where you want to switch from Javascript objects to ES6 classes. For instance, say you have these objects: var MyMixin = { componentShouldUpdate: function() { \/\/ some logic } } React.createClass({ mixins: [MyMixin] getInitialState: function() { \/\/ do some stuff&#8230; }, render: function() { return () &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/converting-javascript-objects-es6-classes\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Converting Javascript objects to ES6 classes&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[302,343,455],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3405"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3405"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3405\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}