Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js : reusable components vs mixin's utility functions

Let's say my react.js app is going to display dates and I would like to apply the format that is set in the client's browser/OS. There are multiple components that show dates and I can use a couple of ways to share the code between them.

1.Re-usable React.js components like:

var React = require('react');

module.exports = React.createClass({
    render : function(){
        if(this.props.value === null || this.props.value===undefined)
            return false;
        var formattedValue =  new Date(this.props.value).toLocaleDateString();
        return(
            <span>
                {formattedValue}
            </span>
        );
    }
}); 

and then use them like:

var DateFormatter = require('./../formatters/Date');
<DateFormatter value={invoice.date} />

2.Utility functions shared via React.js mixins, i.e.:

module.exports = {
    renderDate : function(dateValue){
        if(dateValue === null || dateValue===undefined)
            return false;
        var formattedValue =  new Date(dateValue).toLocaleDateString();
        return(
            <span>
                {formattedValue}
            </span>
        );
    }
} 

and then just add the mixin to a component and use something like

{this.renderDate(invoice.date)}

To me it seems that there is no much difference between these 2 approaches for now. But I would love to hear the opinion of community about pros and cons of each solution. TIA!

like image 798
sovo2014 Avatar asked Mar 19 '15 14:03

sovo2014


People also ask

What is reusable components Reactjs?

In React, a reusable component is a piece of UI that can be used in various parts of an application to build more than one UI instance. For instance, we can have a Button component that displays different texts on different pages.

What are the main 2 types of components you can declare in React?

Components come in two types, Class components and Function components, in this tutorial we will concentrate on Function components. In older React code bases, you may find Class components primarily used.

Can you reuse components in React?

We have our regular functional component in React and then we have the props – name and imageUrl . They're destructured and passed right into our function so we can really reuse this Author component in any section of our application.

In which of the following cases should you use higher-order components?

We use higher order components to primarily reuse logic in React apps. However, they have to render some UI. Hence, HOCs are inconvenient when you want to share some non-visual logic. In such a case, React hooks seem to be a perfect mechanism for code reuse.


1 Answers

One aspect is performance: if you write a component to display the date like you did above, you can mark it with PureRenderMixin, as the rendered output relies only on the the props. This might speed up the rendering a bit, as the formatting only needs to be done when the date changes.

From clean code point of view, I would either write a component or use a non-react utility function that just formats the date to a string - no need to couple the date formatting and dom elements. Something like:

function formatDate(dateValue){
    if(dateValue == null)
        return ""
    return new Date(dateValue).toLocaleDateString()
}
like image 72
OlliM Avatar answered Oct 21 '22 16:10

OlliM