Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 如何在react native中创建全局帮助器函数?_React Native - Fatal编程技术网

React native 如何在react native中创建全局帮助器函数?

React native 如何在react native中创建全局帮助器函数?,react-native,React Native,如何在react native中创建全局帮助器函数 我想使用它访问sqlite数据库或从服务器获取数据。我可以创建一个包含函数的JavaScript文件并从多个视图调用该函数吗?2个方法,不确定哪一个更好: 方法 使用: (不是真正的全局)在您的类中,您可以在需要使用的文件中导出和要求的类中定义您的函数 见此: var React = require('react-native'); var { View, } = React; var styles = StyleSheet.creat

如何在
react native
中创建全局帮助器函数


我想使用它访问sqlite数据库或从服务器获取数据。我可以创建一个包含函数的
JavaScript
文件并从多个视图调用该函数吗?

2个方法,不确定哪一个更好:

  • 方法
  • 使用:

  • (不是真正的全局)在您的
    类中,您可以在
    需要使用的
    文件中导出和要求的
    类中定义您的函数
  • 见此:

    var React = require('react-native');
    
    var {
      View,
    } = React;
    
    var styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
      }
    });
    
    var MoviesScreen = React.createClass({
      foo : function(val) {
        alert(val);
      },
    
    render: function() {
        return (
          <View style={styles.container}>      
          </View>
        );
      },
    });
    
    
    module.exports = MoviesScreen;
    
    var React=require('React-native');
    变量{
    看法
    }=反应;
    var styles=StyleSheet.create({
    容器:{
    弹性:1,
    为内容辩护:“中心”,
    对齐项目:“居中”,
    背景颜色:“#F5FCFF”,
    }
    });
    var MoviesScreen=React.createClass({
    foo:函数(val){
    警报(val);
    },
    render:function(){
    返回(
    );
    },
    });
    module.exports=电影屏幕;
    
    我们过去的做法:

  • 创建导出函数的文件:

    module.exports = function(variable) {    
       console.log(variable);
    }
    
    func('myvariable');
    
  • 在变量中,当要使用该文件时需要该文件:

    var func = require('./pathtofile');
    
  • 使用功能:

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

  • 我有一个助手函数:

    function myfunction(foo)
    
    我在全球宣布:

    global.myfunction = function myfunction(foo) {...};
    

    全局变量

    class MainActivity extends Component {
    
      constructor(){
         super();
    
         // Creating Global Variable.
         global.SampleVar = 'This is Global Variable.';
      }
    }
    
    在第二个活动中

    class SecondActivity extends Component {
    
      render(){
        return(
           <View>
              <Text> {global.SampleVar} </Text>
           </View>
       );
      }
    }
    
    然后导入并使用

    import { TestFunc1 } from './path_to_file'
    

    我是这样做的:

  • 我创建了一个
    helpers.js
    文件,其中将包含我的项目的所有helper函数,并将其放置如下:
    /src/helpers.js
    (您可以将其放置在任何您喜欢的位置)

  • helpers.js
    导出如下函数:

    导出函数globallout(str){….}

  • 然后我导入了如下函数:

    从“../src/helpers”导入{globallout}

  • 最后在文件中使用如下函数:

    globalout(数据)


  • 希望这对任何人都有帮助

    您能演示一下如何进行回调以返回一些结果吗?