10

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

3
  • You can see the answer by @Nader Dabit here: enter link description here Commented Mar 9, 2017 at 8:29
  • 1
    That's Work @zakster But 1. Create a file that exports a function: module.exports = function(variable) { console.log(variable); } Here we can only declare single function , How to declare multiple functions ? Commented Mar 9, 2017 at 8:42
  • 1
    Here is a short and nice answer. stackoverflow.com/a/38402100/2833640 Commented Dec 10, 2017 at 2:05

4 Answers 4

32

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); 
Sign up to request clarification or add additional context in comments.

4 Comments

i use this code but showing undefined is not an object (evaluating _ApiHelper.ApiHelper.getdata)
Hi @AslamPatel, Can you share the code so I can help you find out?
Thanks, similar approach utils directory having a variety of helpers such as validation, working with dates, and so on. Good approach!
Shouldn't this read import dateTimeHelper and not import DateTimeHelper or vice versa?
0

Create a file at application root named as Common.js and inside common.js file add this:

'use strict'; class Common { //here you can use your validation email code } module.exports = Common; 

Now access this class wherever you want.

1 Comment

How to access the functions inside the Common.js @Ankush Rishi
0

For the first question as I wrote in the comments you can use this:

Export one function

If you want to export many functions you can use this:

Export many functions

You can use the same technique for other things also like styles, fonts, colors and so on.

These questions are duplicates and for this reason I add the links here.

Comments

0

I have created reuseable function as below:

Helper.js import { Platform, Dimensions } from "react-native";

function isIphoneWithNotch() { const dimen = Dimensions.get("window"); return ( Platform.OS === 'ios' && !Platform.isPad && !Platform.isTVOS && ( dimen.height === 780 || dimen.width === 780 || dimen.height === 812 || dimen.width === 812 || dimen.height === 844 || dimen.width === 844 || dimen.height === 896 || dimen.width === 896 || dimen.height === 926 || dimen.width === 926) ); } export { isIphoneWithNotch }; 

Use this function:

import { isIphoneWithNotch } from "./Helper"; console.log(isIphoneWithNotch()); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.