当用户点击按钮时,我正在尝试打开一个上传文档对话框。我从另一个文件(“ upload-recording-button.js”)导入要执行此功能的函数,然后尝试在按钮中使用它。但是,我遇到了许多错误,例如“ _uploadRecordingButton.uploadRecording未定义。”
根据其他相关的Stack Overflow页面,我尝试将按钮的OnPress更改为this.uploadRecording,uploadRecording(this),this.uploadRecording()以及其他一些变体。它们都因“功能不存在”而失败。
这是文件中包含我正在调用的功能的一部分:
upload-recording-button.js
import React, {Component} from 'react';
import { DocumentPicker } from 'expo';
async function uploadRecording() {
let result = await DocumentPicker.getDocumentAsync({type: '*/*'});
if (!result.cancelled) {
//upload function(result.uri);
}
}
export default uploadRecording;
主文件
import { uploadRecording } from '../components/upload-recording-button';
export default class HomeScreen extends React.Component {
static navigationOptions = {
//header: null,
title: 'Recents',
// uploadRecording(this)
headerRight: (
<Button
onPress={uploadRecording()}
title="Add"
color="#007AFF"
/>
),
};
最佳答案
您需要从upload-recording-button.js
导出功能
import React, {Component} from 'react';
import { DocumentPicker } from 'expo';
export async function uploadRecording() {
let result = await DocumentPicker.getDocumentAsync({type: '*/*'});
if (!result.cancelled) {
//upload function(result.uri);
}
}
如果您要从
upload-recording-button.js
导出的功能只有一个,则可以使用默认导出export default function-name
并将其导入您要使用的文件中
import anyName from file-fath