Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending React.js components

One of the things I appreciate the most about Backbone.js is how simple and elegant inheritance works. I'm starting to get to grips with React, and can't really find anything in react that resembles this Backbone code

var Vehicle = Backbone.View.extend({     methodA: function() { // ... }     methodB: function() { // ... }     methodC: function() { // ... } });  var Airplane = Vehicle.extend({     methodC: function() {         // Overwrite methodC from super     } }); 

In react we have mixins, and using those we could get somewhat close to the above example if we went like this

var vehicleMethods = {     methodA: function() { // ... }     methodB: function() { // ... } }  var Vehicle = React.createClass({     mixins: [vehicleMethods]     methodC: function() {          // Define method C for vehicle     } });  var Airplane = React.createClass({     mixins: [vehicleMethods]     methodC: function() {         // Define method C again for airplane     } }); 

This is less repetitive than defining the same stuff over and over again, but it doesn't seem to be nearly as flexible as the Backbone way. For instance, I get an error if I try to redefine/overwrite a method that exists in one of my mixins. On top of that, the React.js way is more code for me to write.

There is some incredibly clever stuff in react, and it feels like this is more the case of me not getting how to properly do this, than it feels like a feature missing from React.

Any pointers are greatly appreciated.

like image 733
Ahrengot Avatar asked Sep 08 '14 08:09

Ahrengot


People also ask

How do you extend a functional component in React?

A simple approach is to move your helpful functions outside of the function components, so that they can be used by multiple components. These could be in a utilities file that you import from Home. jsx and APIHome. jsx , but here I've coded them as if they're all in one file for simplicity.

Why do we Extends component in React?

Extending Component will give you access to functions like componentDidMount , componentDidUpdate , componentWillUnmount and render . Show activity on this post. if you extend React. Component class , you can use the life cycle of the Component which can be used for mounting and unmounting.

What is the extension of React component?

It is called JSX, and it is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript. JSX produces React “elements”.

Can you do components inheritance in React?

React has a powerful composition model, and is recommend to use composition instead of inheritance to reuse code between components. However, there can be cases where inheritance is preferred to composition like reusing a particular functionality of a component than the component itself.


1 Answers

To get something that resembles inheritance (actually composition as pointed out in comments), you can make an Airplane in your example wrap itself in a Vehicle. If you want to expose methods on Vehicle in the Airplane component, you can use a ref and connect them one-by-one. This is not exactly inheritance (it's actually composition), particularly because the this.refs.vehicle will not be accessible until after the component has been mounted.

var Vehicle = React.createClass({     ... });  var Airplane = React.createClass({     methodA: function() {       if (this.refs != null) return this.refs.vehicle.methodA();     },     ...     render: function() {         return (             <Vehicle ref="vehicle">                 <h1>J/K I'm an airplane</h1>             </Vehicle>         );     } }); 

Also it's worth mention that in the React official documentation they prefer composition over inheritance:

So What About Inheritance? At Facebook, we use React in thousands of components, and we haven't found any use cases where we would recommend creating component inheritance hierarchies.

Props and composition give you all the flexibility you need to customize a component's look and behavior in an explicit and safe way. Remember that components may accept arbitrary props, including primitive values, React elements, or functions.

If you want to reuse non-UI functionality between components, we suggest extracting it into a separate JavaScript module. The components may import it and use that function, object, or a class, without extending it.

Another thing worth mention that using ES2015/ES6+ you can also spread the object props from Airplane component to the Vehicle component

render: function() {     return (         <Vehicle {...this.props}>             <h1>J/K I'm an airplane</h1>         </Vehicle>     ); } 
like image 97
Ross Allen Avatar answered Sep 23 '22 03:09

Ross Allen