我有一个带有默认道具的react组件

Component.defaultProps = {
  waitForResolve: defaultFunction
}


defaultFunction接受event作为参数,并返回拖动到浏览器中的对象数组。在那之后的某个时刻,组件具有一个Promise,它将等待该函数解析并继续进行其他操作。

尽管此代码有效(我导入了importedFunction),但我无法弄清楚如何使其适用于默认功能,因此我得到的结果相同,但没有提供除默认功能外的任何其他功能。

import {importedFunction} from 'some-package'

<Component
   waitForResolve={evt =>
     importedFunction(evt).then(files => files.map(({ file, ...rest }) => file))
   }
>
   <p>Try dropping a folder here.</p>
</Component>


我需要类似上面的代码的内容,但将importedFunction替换为defaultFunction

感谢帮助!

最佳答案

这个怎么样:

import React from 'react';
import { importedFunction } from 'some-package';

const defaultFunction = evt => importedFunction(evt).then(files => files.map(({ file, ...rest }) => file));

export default class Component extends React.Component {
  static defaultProps = {
    waitForResolve: defaultFunction
  }

  render() {
    return <input onChange={this.props.waitForResolve} type="file" />;
  }
}


简单用法:

<Component />

09-25 17:36