我有两个js文件。一种带有AComponent的代码。在第二个js中,我导入了AComponent文件,此外,我写了:
const Ctext = (props) => (....)
如何同时导出?
import React, { Component } from 'react';
import AComponent from "./image";
const Ctext = (props) => (
<div>
<h5 className="card-title">Image n Text Card</h5>
<p className="card-text">{props.dataFromParent.textContent}</p>
</div>
);
export default Ctext;
export const props => <AComponent />;
您无法导出未命名的函数,必须对其进行命名,否则无法将其导入其他文件:
const myFunc = props => <AComponent>
然后您可以导出myFunc。
我认为最好只导出一个组件,但是如果您希望导出多个组件或函数,我会将它们分组到一个objext中,然后将该对象导出为默认导出:
const objectToExport = { myFunc, myComponent };
export default objectToExport
也请注意,每个文件只能有一个默认导出
此行可能会给您带来问题
export const props => <AComponent />;
您没有为要导出的函数定义名称。您可以尝试,
export const aComponentFunction = props => <AComponent {...props} />; //<-- I added the props too
您无法导出未命名的函数,必须对其进行命名,否则无法将其导入其他文件:
const myFunc = props => <AComponent>
然后您可以导出myFunc。
我认为最好只导出一个组件,但是如果您希望导出多个组件或函数,我会将它们分组到一个objext中,然后将该对象导出为默认导出:
const objectToExport = { myFunc, myComponent };
export default objectToExport
也请注意,每个文件只能有一个默认导出