【发布时间】:2016-11-19 00:18:55
【问题描述】:
虽然有一个类似的问题,但我无法创建具有多个功能的文件。不确定该方法是否已经过时,因为 RN 的发展非常迅速。 How to create global helper function in 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,这意味着我正在导入正确的路径,而不是其他任何路径。
请问正确的做法是什么?
【问题讨论】:
标签: reactjs react-native