How to create helper file full of functions in react native?

后端 未结 6 752
悲哀的现实
悲哀的现实 2020-12-04 06:48

Though there is a similar question I am failing to create a file with multiple functions. Not sure if the method is already outdated or not as RN is evolving very fast. How

6条回答
  •  被撕碎了的回忆
    2020-12-04 07:20

    An alternative is to create a helper file where you have a const object with functions as properties of the object. This way you only export and import one object.

    helpers.js

    const helpers = {
        helper1: function(){
    
        },
        helper2: function(param1){
    
        },
        helper3: function(param1, param2){
    
        }
    }
    
    export default helpers;
    

    Then, import like this:

    import helpers from './helpers';
    

    and use like this:

    helpers.helper1();
    helpers.helper2('value1');
    helpers.helper3('value1', 'value2');
    

提交回复
热议问题