如何在 React Native 中创建充满功能的帮助文件?

IT技术 reactjs react-native
2021-03-27 04:38:34

尽管有一个类似的问题,但我无法创建具有多种功能的文件。不确定该方法是否已经过时,因为 RN 发展非常快。如何在 React Native 中创建全局辅助函数?

我是 React Native 的新手。

我想要做的是创建一个包含许多可重用函数的 js 文件,然后将其导入到组件中并从那里调用它。

到目前为止我一直在做的事情可能看起来很愚蠢,但我知道你会要求它,所以他们在这里。

我尝试创建一个类名 Chandu 并像这样导出

'use strict';
import React, { Component } from 'react';
import {
  AppRegistry,
  Text,
  TextInput,
  View
} from 'react-native';


export default class Chandu extends Component {

  constructor(props){
    super(props);
    this.papoy = {
      a : 'aaa'
    },
    this.helloBandu = function(){
      console.log('Hello Bandu');
    },
  }

  helloChandu(){
    console.log('Hello Chandu');
  }
}

然后我将它导入到任何需要的组件中。

import Chandu from './chandu';

然后这样称呼

console.log(Chandu);
console.log(Chandu.helloChandu);
console.log(Chandu.helloBandu);
console.log(Chandu.papoy);

唯一有效的是第一个 console.log,这意味着我正在导入正确的路径,而不是任何其他路径。

请问这样做的正确方法是什么?

6个回答

快速说明:您正在导入一个类,除非它们是静态属性,否则不能调用类上的属性。在此处阅读有关课程的更多信息:https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

不过,有一种简单的方法可以做到这一点。如果您正在制作辅助函数,您应该制作一个导出函数的文件,如下所示:

export function HelloChandu() {

}

export function HelloTester() {

}

然后像这样导入它们:

import { HelloChandu } from './helpers'

或者...

import functions from './helpers' 然后 functions.HelloChandu

那么导出一个包含一堆函数的对象呢?另外,导出这样的对象与导出具有静态属性的类的优缺点是什么?
2021-05-27 04:38:34
从风格上讲,js 中的函数不是通常是“小驼峰式”吗?
2021-05-28 04:38:34
好的,我知道了,谢谢。必须从这里阅读一些explorejs.com/es6/ch_modules.html
2021-05-30 04:38:34
像我们在这里一样使用命名导出只是一个被导出的对象。这就是您可以对导入进行解构的原因。import functions from './helpers'functions. HelloChandu将在那里。函数是一个包含所有函数的对象。在此处阅读有关导出的信息 :) developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/...
2021-06-08 04:38:34
在一个类上使用一堆静态属性的缺点是你无缘无故地拥有一个类。这就像使用一个你不需要的 api。为什么要new为静态属性创建一个类?在这种情况下导出函数
2021-06-13 04:38:34

另一种方法是创建一个帮助文件,其中您有一个 const 对象,其中包含函数作为对象的属性。这样您只需导出和导入一个对象。

helpers.js

const helpers = {
    helper1: function(){

    },
    helper2: function(param1){

    },
    helper3: function(param1, param2){

    }
}

export default helpers;

然后,像这样导入:

import helpers from './helpers';

并像这样使用:

helpers.helper1();
helpers.helper2('value1');
helpers.helper3('value1', 'value2');
如果您想从单个module/对象直接访问方法,则可以使用这种方法。谢谢!
2021-06-02 04:38:34
@约翰试试 helper2: function(param1){ helpers.helper1(); }
2021-06-07 04:38:34
我知道已经有一段时间了,但有一个后续问题:有没有一种巧妙的方法可以从另一个辅助函数中调用一个辅助函数?即 helper2: function(param1){ helper1(); ? 我尝试过 this.helper1() 和 helper1() 但都没有奏效。
2021-06-09 04:38:34
我喜欢这个。声明为对象并在点之后公开方法。更好地使用智能感知自动导入以加快开发速度。
2021-06-15 04:38:34

我相信这会有所帮助。在目录中的任意位置创建 fileA 并导出所有函数。

export const func1=()=>{
    // do stuff
}
export const func2=()=>{
    // do stuff 
}
export const func3=()=>{
    // do stuff 
}
export const func4=()=>{
    // do stuff 
}
export const func5=()=>{
    // do stuff 
}

在这里,在你的 React 组件类中,你可以简单地编写一个 import 语句。

import React from 'react';
import {func1,func2,func3} from 'path_to_fileA';

class HtmlComponents extends React.Component {
    constructor(props){
        super(props);
        this.rippleClickFunction=this.rippleClickFunction.bind(this);
    }
    rippleClickFunction(){
        //do stuff. 
        // foo==bar
        func1(data);
        func2(data)
    }
   render() {
      return (
         <article>
             <h1>React Components</h1>
             <RippleButton onClick={this.rippleClickFunction}/>
         </article>
      );
   }
}

export default HtmlComponents;
如果我想使用 this.props.action 在 func1 中调用 redux 操作...我如何更改 React 组件类中的代码?我得到 undefined 不是一个对象(评估'_this.props.actions')
2021-05-25 04:38:34
@DinIslamMilon 只是我的偏好。如果我在单独的文件/module中有函数。我会将它们作为对象的常量或属性。我不使用直接函数或导出直接函数。我不认为使用其他方式有任何伤害
2021-06-05 04:38:34
我得到了你想要在这里实现的目标。我可以建议的是将回调函数传递给 func1。在回调函数中,您可以使用 this.props.action 调度您的操作。您需要记住的另一件事是您必须映射DispatchToProps,我希望您正在这样做。
2021-06-20 04:38:34
为什么是常量?函数名称之前的导出关键字有什么区别吗?
2021-06-20 04:38:34

为了通过文件实现您想要的并更好地组织,您可以创建一个 index.js 来导出您的帮助文件。

假设您有一个名为/helpers的文件夹在此文件夹中,您可以创建按内容、操作或任何您喜欢的内容划分的功能。

例子:

/* Utils.js */
/* This file contains functions you can use anywhere in your application */

function formatName(label) {
   // your logic
}

function formatDate(date) {
   // your logic
}

// Now you have to export each function you want
export {
   formatName,
   formatDate,
};

让我们创建另一个文件,该文件具有帮助您处理表格的功能:

/* Table.js */
/* Table file contains functions to help you when working with tables */

function getColumnsFromData(data) {
   // your logic
}

function formatCell(data) {
   // your logic
}

// Export each function
export {
   getColumnsFromData,
   formatCell,
};

现在的诀窍是在helpers文件夹中有一个 index.js

/* Index.js */
/* Inside this file you will import your other helper files */

// Import each file using the * notation
// This will import automatically every function exported by these files
import * as Utils from './Utils.js';
import * as Table from './Table.js';

// Export again
export {
   Utils,
   Table,
};

现在您可以分别导入 then 以使用每个函数:

import { Table, Utils } from 'helpers';

const columns = Table.getColumnsFromData(data);
Table.formatCell(cell);

const myName = Utils.formatName(someNameVariable);

希望它可以帮助以更好的方式组织您的文件。

如果你想使用类,你可以这样做。

Helper.js

  function x(){}

  function y(){}

  export default class Helper{

    static x(){ x(); }

    static y(){ y(); }

  }

应用程序.js

import Helper from 'helper.js';

/****/

Helper.x