【问题标题】:Can't track down possible import/export error - React/ES6 modules无法追踪可能的导入/导出错误 - React/ES6 模块
【发布时间】:2019-08-03 00:01:16
【问题描述】:

我创建了一个围绕反应包 (react-datepicker) 的包装器,以便在我的团队中的产品之间拥有一个有凝聚力的日期选择器。我已经编写了包装器,可以在其中传入要呈现的自定义输入组件。我已经对其进行了测试,它可以在 Storybook 中使用。

我现在正尝试将此包装器绑定到 Rails 应用程序中。我有一个构建可以插入 Rails 应用程序的组件的存储库(适用于其他组件)。

尝试渲染组件时,出现以下错误:

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check your code at index.js:8.
    in DatePicker (at renderComponent.js:5)

我已经搜索过了,答案似乎是我有可能在没有{} 的情况下导入了命名导出或使用{} 导入了默认导出。

我已经三次检查了我的代码,但找不到可能发生这种情况的地方。

以下是我的包装代码。

FVDatePicker.js

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import DatePicker from 'react-datepicker'
import 'react-datepicker/dist/react-datepicker.css'
import FVPicker from './FVDatePickerStyles'

class FVDatePicker extends Component {
  constructor(props) {
    super(props)

    this.state = {
      selectedDate: ''
    }
  }

  componentDidMount() {
    this.setState({ selectedDate: this.props.date || new Date() })
  }

  changeDateValue = selectedDate => {
    this.setState({ selectedDate })
  }

  render() {
    // This check and subsequent throw is because PropTypes
    // doesn't stop the execution of the component
    if (!this.props.customInput) {
      throw "'customInput' prop is required"
    }

    return (
      <FVPicker className="FVDatePicker">
        <DatePicker
          customInput={this.props.customInput}
          selected={this.state.selectedDate}
          onChange={date => this.changeDateValue(date)}
          dateFormat={this.props.dateFormat || 'MM/dd/yyyy'}
        />
      </FVPicker>
    )
  }
}

FVDatePicker.propTypes = {
  customInput: PropTypes.node.isRequired
}

export default FVDatePicker

对应的样式FVDatePickerStyles.js使用react-emotion

import styled from 'react-emotion'
import DT from 'design-tokens/src/design-tokens'

const FVPicker = styled.div/* css */ `
  display: inline-block;

  // Other styles but leaving out for the sake of brevity

`

export default FVPicker

然后将上面的代码汇总到dist/index.js

在我的可渲染应用程序中,代码如下。

componentName/index.js

import React from 'react'
import FVDatePicker from 'fv-datepicker'
import renderComponent from '../../utils/renderComponent'
import CustomDatePickerInput from './CustomDatePickerInput'

const DatePicker = () => {
  return (
    <FVDatePicker
      date=""
      inputName="start-time"
      dateFormat="yyyy/MM/dd"
      customInput={<CustomDatePickerInput />}
    />
  )
}

window.DatePicker = renderComponent(DatePicker)
export default DatePicker

FVDatePicker 来自 node_modules,因为它在 package.json

这就是上面文件中引用的renderComponent:

import React from 'react'
import { render } from 'react-dom'

const renderComponent = Component => (element, props = {}) =>
  render(<Component {...props} />, element)

export default renderComponent

预期的结果应该是我的组件应该在 rails 应用程序中呈现,但会抛出上面列出的错误(我将在此处再次添加):

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check your code at index.js:8.
    in DatePicker (at renderComponent.js:5)

我在导入/导出方面遗漏了什么吗?还是做错了什么?

【问题讨论】:

  • FVDatePicker 是默认导入还是命名导入?尝试从 'fv-datepicker' 导入 {FVDatePicker }
  • @PouyaAtaei,谢谢。这使它起作用了!现在我只需要弄清楚为什么我正在包装的包中的 CSS import 'react-datepicker/dist/react-datepicker.css' 不起作用。 CSS 完全不存在似乎很奇怪。我想知道这是否是一个摇树问题。
  • 你确定你的 CSS 文件在那里吗?此外,一旦所有问题都解决了,请告诉我,我会发布答案......干杯
  • @PouyaAtaei,是的,CSS 就在那里。我弄清楚了这个问题。它是用于汇总的 PostCss 插件。 modules 标志设置为 true,它正在散列我的 CSS 选择器,导致 CSS 不绑定到元素。一旦我删除了{modules: true,它就按计划工作了。谢谢您的帮助。我们在这里很好!
  • 您介意将我的答案标记为解决方案吗?

标签: javascript reactjs ecmascript-6 import export


【解决方案1】:

FVDatePicker默认导入还是命名导入?试试

import { FVDatePicker } from 'fv-datepicker'

当导入错误时,通常会发生这种反应错误,并且很容易忽略...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 2017-06-29
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 2016-05-22
    相关资源
    最近更新 更多