cjm*_*ing 110 reactjs react-native
虽然有类似的问题,但我无法创建具有多个功能的文件.不确定该方法是否已经过时,因为RN正在快速发展.如何在本机中创建全局辅助函数?
我是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');
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将其导入任何所需的组件.
import Chandu from './chandu';
Run Code Online (Sandbox Code Playgroud)
然后像这样称呼它
console.log(Chandu);
console.log(Chandu.helloChandu);
console.log(Chandu.helloBandu);
console.log(Chandu.papoy);
Run Code Online (Sandbox Code Playgroud)
唯一有效的是第一个console.log,这意味着我正在导入正确的路径,但不会导入任何其他路径.
请问这样做的正确方法是什么?
zac*_*ify 159
快速注意:您正在导入类,除非它们是静态属性,否则不能在类上调用属性.在此处阅读有关课程的更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
不过,有一种简单的方法可以做到这一点.如果要创建辅助函数,则应该创建一个导出如下函数的文件:
export function HelloChandu() {
}
export function HelloTester() {
}
Run Code Online (Sandbox Code Playgroud)
然后像这样导入它们:
import { HelloChandu } from './helpers'
c-c*_*vez 61
另一种方法是创建一个辅助文件,其中有一个const对象,其中函数作为对象的属性.这样您只能导出和导入一个对象.
helpers.js
const helpers = {
helper1: function(){
},
helper2: function(param1){
},
helper3: function(param1, param2){
}
}
export default helpers;
Run Code Online (Sandbox Code Playgroud)
然后,像这样导入:
import helpers from './helpers';
Run Code Online (Sandbox Code Playgroud)
并使用这样的:
helpers.helper1();
helpers.helper2('value1');
helpers.helper3('value1', 'value2');
Run Code Online (Sandbox Code Playgroud)
han*_*man 20
我相信这可以提供帮助.在目录中的任何位置创建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
}
Run Code Online (Sandbox Code Playgroud)
在这里,在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;
Run Code Online (Sandbox Code Playgroud)
Ita*_*ges 10
为了实现所需的文件并通过文件进行更好的组织,可以创建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,
};
Run Code Online (Sandbox Code Playgroud)
让我们创建另一个文件,该文件具有帮助您处理表的功能:
/* 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,
};
Run Code Online (Sandbox Code Playgroud)
现在的诀窍是在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,
};
Run Code Online (Sandbox Code Playgroud)
现在,您可以分别导入以使用每个功能:
import { Table, Utils } from 'helpers';
const columns = Table.getColumnsFromData(data);
Table.formatCell(cell);
const myName = Utils.formatName(someNameVariable);
Run Code Online (Sandbox Code Playgroud)
希望它可以帮助您更好地组织文件。
归档时间: |
|
查看次数: |
106817 次 |
最近记录: |