我如何获得这个React方法来返回一个语句/变量?

时间:2019-05-07 13:16:46

标签: javascript html reactjs

这是JobInfoPage.js文件的摘录

import findMonthsTillSession from '../components/utility.js';

class JobInfoPage extends Component {
    render() {
            <div>The nearest hiring session is in: { findMonthsTillSession() } See you then!</div>
    }
}

我收到此错误: Attempted import error: '../components/utility.js' does not contain a default export (imported as 'findMonthsTillSession').

3 个答案:

答案 0 :(得分:0)

您的问题有2个错误,
(1)No return() in JobInfoPage.js文件
(2)Function is not imported with function name.
JobInfoPage.js

import findMonthsTillSession from '../components/utility.js';

class JobInfoPage extends Component {
    render() {
       return(
       <div>
            The nearest hiring session is in: { findMonthsTillSession() } See you then!
       </div>
       )
    }
}

如果尚未添加,请在Utility.js中添加import React from 'react';

也请阅读hackernoon export import cheatsheet

答案 1 :(得分:0)

您需要将React导入您的Utility.js

import React from 'react';

export function FindMonthsTillSession() {
    return <p> 5 months </p>;
}

然后按如下所示导入实用程序:

import FindMonthsTillSession from '../components/utility

答案 2 :(得分:0)

感谢大家的帮助!我猜因为自从我导出了默认的 ,所以我不得不在导入文件上放花括号,就像这样:

import { findMonthsTillSession } from '../components/utility.js';

现在是计算月份的难点!