Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In react native how to reuse functions?

I need access functions from another page in react native .

Example :

validateEmail = (email) => {
// ---- Code ----
}

I need access that function in both login.js and registration.js

like image 732
Saravana Kumar Avatar asked Mar 09 '17 08:03

Saravana Kumar


1 Answers

I can give you a quick example which I use in my current project.

You can follow the steps:

1.Create a /utils folder which you can put all the shared functions files for example datetimeHelper.js

const dateTimeHelper = {
  getFormattedDatetime: (datetime) => {
    return moment.utc(datetime).local().format('MMM Do, YYYY, h:mm a');
  }
}

export default datetimeHelper;

2.Import the file into where you need it:

import datetimeHelper from './utils/datetimeHelper.js';

3 You can call the functions:

datetimeHelper.getFormattedDatetime(MY_DATETIME);
like image 59
MattYao Avatar answered Nov 23 '22 05:11

MattYao