This is a Livecoding Recap – an almost-weekly post about interesting things discovered while livecoding ?. Always under 500 words and with pictures. You can follow my channel, here. New content almost every Sunday at 2pm PDT. There’s live chat, come say hai ?
Today we battled with a fearsome error, an error of odd implications and no clear solutions. Uncaught TypeError: Super expression must either be null or function
What does that even mean!? ?
Well, it means that when you import TransitionableComponent from 'react-transitionable-component'
, you get an object instead of a function. We confirmed the problem with some console.log calls. After importing TransitionableComponent
is an object.
It looks just like a React component is supposed to. There’s a constructor method, a bunch of default object methods, and – I assume – all the Component
methods as well. That’s great when you want to use a component in your render()
function. Not so great when you want to use it as a parent class.
When you do something like class Arc extends TransitionableComponent
, it fails. You can extend a null
or a function
, but not an object.
You can inherit from a class, but not from an instance of a class. I’m sure it’s like that in every language, but the reason it’s like that in JavaScript is that class TransitionableComponent extends Component
transpiles into:
var TransitionableComponent = (function (_Component) {
_inherits(TransitionableComponent, _Component);
function TransitionableComponent(props) {
_classCallCheck(this, TransitionableComponent);
// I think this is super(props)
var _this = _possibleConstructorReturn(
this,
Object.getPrototypeOf(TransitionableComponent).call(this, props)
);
// this is where your constructor body goes
return _this;
}
_createClass(TransitionableComponent, [
{
// this is where your class body goes
},
]);
return TransitionableComponent;
})(_react.Component);
That _inherits
call is the crucial piece. It does a bunch of .prototype
magic to extend the definition of a given class with the definition of a child class. Instances don’t have prototypes, functions do.
Functions have a prototype
property because of legacy reasons, I’m sure. That’s how JavaScript has always understood the concept of classes – generator/constructor functions double as classes.
At this point TransitionableComponent
is a function - just like we’d expect. With some console.log
-ing we confirmed that it remains a function right until the point we import it in our sample project.
Hmmm ?
I don’t know why it becomes an object. Our guessing and prodding didn’t reveal much. This is what importing transpiles to:
var _TransitionableComponent = __webpack_require__(16);
var _TransitionableComponent2 = _interopRequireDefault(
_TransitionableComponent
);
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { default: obj };
}
This code might return an object, if you’re importing something that isn’t an ES6 Module. But we know that’s not our problem because TransitionableComponent.default
is undefined.
You’d think __webpack_require__
was instantiating our component and returning an object instead of a function, but it works correctly when you import React’s default Component
. Curiously, React’s compiled code looks like normal ES5 without even a hint of Webpack or Babel. Hmmm ?
I am at a loss. I have no idea what’s going on or why. But until we figure this out, react-transitionable-component
will not be a usable library and my chance at open source glory lays trampled in the wastelands of npmjs.com. ?
Ideas?
PS: the edited and improved versions of these videos are becoming a video course. Readers of the engineer package of React+d3js ES6 get the video course for free when it’s ready.
Continue reading about Livecoding #20: You can’t extend an object
Semantically similar articles hand-picked by GPT-4
- Livecoding #21: Use Babel for libraries, not Webpack
- Livecoding #18: An abstract React transition component
- Livecoding #19: It’s hard to package a library
- Livecoding #27: New React Indie Bundle page almost done
- This is Yak Shaving
Learned something new?
Read more Software Engineering Lessons from Production
I write articles with real insight into the career and skills of a modern software engineer. "Raw and honest from the heart!" as one reader described them. Fueled by lessons learned over 20 years of building production code for side-projects, small businesses, and hyper growth startups. Both successful and not.
Subscribe below 👇
Software Engineering Lessons from Production
Join Swizec's Newsletter and get insightful emails 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.
"Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"
Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.
Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.
Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.
Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev
Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization
Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections
Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog
Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️