【问题标题】:Export custom class or function in React-Native在 React-Native 中导出自定义类或函数
【发布时间】:2017-02-20 10:32:41
【问题描述】:

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

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

下面是我的代码:

customdata.js

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

row.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);

我的控制台日志

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

【问题讨论】:

  • 你是最棒的!!非常感谢,希望我能支持这个答案:D

标签: react-native ecmascript-6


【解决方案1】:

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

import { clienList } from './customdata';
clienList(); // this will return the value
clienList // this will return the function

【讨论】:

猜你喜欢
  • 2020-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
  • 2018-10-03
  • 2015-06-02
相关资源
最近更新 更多