如何在 React Native 中创建全局辅助函数? [英] How to create global helper function in react native?

查看:52
本文介绍了如何在 React Native 中创建全局辅助函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 react-native 中创建 Global Helper 函数?

How can I create Global Helper functions in react-native?

我想用于访问 sqlite 数据库或从服务器获取数据.我可以创建带有函数的 JavaScript 文件并从多个视图调用该函数吗?

I would like to use for accessing sqlite database or fetching data from server. Can I create a JavaScript file with functions and call that functions from multiple views?

推荐答案

我们过去的做法:

  1. 创建一个导出函数的文件:

  1. Create a file that exports a function:

module.exports = function(variable) {    
   console.log(variable);
}

  • 当你想使用它时需要文件,在一个变量中:

  • Require the file when you want to use it, in a variable:

    var func = require('./pathtofile');
    

  • 使用函数:

  • Use function:

    func('myvariable');
    

  • 这篇关于如何在 React Native 中创建全局辅助函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆