尽管有一个类似的问题,但我无法创建具有多种功能的文件。不确定该方法是否已经过时,因为 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,这意味着我正在导入正确的路径,而不是任何其他路径。
请问这样做的正确方法是什么?