在 React-Native 中导出自定义类或函数

Son*_*nke 1 ecmascript-6 react-native

如何在 React-Native 中导出和使用自定义的类或函数?

我创建了一个名为“ customdata.js ”的文件 ”,其中包含一个返回数组数据的函数。当我从该文件导入函数时,我无法检索返回值。它只在控制台日志中返回 [Function: function-name]

这是我的代码如下:

自定义数据.js

export function clienList(){
  let clients = [
    'D4 Soweto',
    'Road Freight Provident Fund',
    'Internal Cafe'
  ];
  return clients;
}
Run Code Online (Sandbox Code Playgroud)

行.js

//import from customdata
import {clienList} from './customdata';

//ListView
const elems = ['something'];
const source = new ListView.DataSource({
                   rowHasChanged: (r1, r2) => r1 !== r2
                });

export default class Row extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      dataSource: source.cloneWithRows(elems),
      pickerValue:'Sonke'
    };
  };
  render() {
    console.log(clienList);
Run Code Online (Sandbox Code Playgroud)

我的控制台日志

02-20 12:21:30.853 2844 3730 我 ReactNativeJS:[函数:clienList]

kno*_*ody 5

只需调用该函数。正如您在控制台日志中看到的,您正在导入函数,为了让函数返回一个值,您需要先调用它clienList()

import { clienList } from './customdata';
clienList(); // this will return the value
clienList // this will return the function
Run Code Online (Sandbox Code Playgroud)