{"id":3416,"date":"2016-03-15T21:47:14","date_gmt":"2016-03-15T21:47:14","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3416"},"modified":"2016-03-15T21:47:14","modified_gmt":"2016-03-15T21:47:14","slug":"fixing-react-error-uncaught-typeerror-mymixin-not-functionanonymous-function","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/fixing-react-error-uncaught-typeerror-mymixin-not-functionanonymous-function\/","title":{"rendered":"Fixing React error: Uncaught TypeError: MyMixin is not a function(anonymous function)"},"content":{"rendered":"<p>When upgrading React, you may be tempted to have your ES6 classes extend Mixin objects (to avoid upgrading these)<\/p>\n<p>For example:<\/p>\n<pre lang=\"javascript\">\nvar MyMixin = {\n  componentShouldUpdate: function() {\n    \/\/ some logic\n  }\n}\n\nclass MyComponent extends MyMixin {\n  constructor() {\n    \/\/ do some stuff...\n    this.state = {}\n  }\n\n  render() { \n    return (<div><\/div>)\n  }\n})\n<\/pre>\n<p>You will get a very cryptic error like so:<\/p>\n<pre lang=\"Javascript\">\nUncaught TypeError: MyMixin is not a function(anonymous function)\n<\/pre>\n<p>This is because &#8220;objects&#8221; are actually a function (a constructor) with the information for how to construct the object in fn._prototype.<\/p>\n<p>To fix the above error, you can either convert MyMixin to a real class, or make a base class that you can extend that contains the mixins, like so:<\/p>\n<pre lang=\"javascript\">\nclass MyComponent extends 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>When upgrading React, you may be tempted to have your ES6 classes extend Mixin objects (to avoid upgrading these) For example: var MyMixin = { componentShouldUpdate: function() { \/\/ some logic } } class MyComponent extends MyMixin { constructor() { \/\/ do some stuff&#8230; this.state = {} } render() { return () } }) You &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/fixing-react-error-uncaught-typeerror-mymixin-not-functionanonymous-function\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Fixing React error: Uncaught TypeError: MyMixin is not a function(anonymous function)&#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":[9],"tags":[302,343,455],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3416"}],"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=3416"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3416\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}