How to export method and variables in react?

How to use globally any method or variable in react?

Need to create a helper.js file or as you wish according to your requirement. Then create method or variable but remember that use “export” keyword before function or variable as below code:

Helper.js 
export var myCompanyName = 'https://www.anythinglearn.com/'
export var baseUrl = 'http://localhost:3009/'

export function days(day) {
    if (day === '1') {
        return 'Monday';
    } else if (day === '2') {
        return 'Tuesday';
    } else if (day === '3') {
        return 'Wednesday';
    } else if (day === '4') {
        return 'Thursday';
    } else if (day === '5') {
        return 'Friday';
    } else if (day === '6') {
        return 'Saturday';
    } else if (day === '7') {
        return 'Sunday';
    } else {
        return 'Good Day';
    }
}


var theMonths = ["January""February""March""April""May",
    "June""July""August""September""October""November""December"];

export function dateFormates(date) {
    var today = new Date(date);
    var dd = today.getDate();
    var mm = today.getMonth(); //January is 0!

    var yyyy = today.getFullYear();

    mm = theMonths[mm];

    return mm + ' ' + dd + ' ' + yyyy;

}

 import {myCompanyNamefrom '../assets/js/helpers'

{dateFormates(this.props.createDate)}
axios.put(baseUrl+'api/'+iddetailObject)


No comments:

Note: Only a member of this blog may post a comment.

Copyright Reserved to Anything Learn. Powered by Blogger.