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
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
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); utils directory having a variety of helpers such as validation, working with dates, and so on. Good approach!import dateTimeHelper and not import DateTimeHelper or vice versa?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.
For the first question as I wrote in the comments you can use this:
If you want to export many functions you can use this:
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.
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());