【问题标题】:React AntD Forms Error Element type is invalid: expected a string (for built-in components)React AntD Forms 错误元素类型无效:预期为字符串(对于内置组件)
【发布时间】:2018-10-19 02:51:27
【问题描述】:

我在尝试使用 AntD 表单时遇到了这个错误:

Element type is invalid: expected a string (for built-in components)

我做过研究,很多话题都在讨论如何导入/导出组件

但是,我不确定这是否是我的问题。这是我的组件:

**** 注册组件 ****

import React, { Component } from 'react'

/** UI Framework Components **/
import { Button, Form, Icon, Input } from 'antd'

class FormWrapper extends Component {
render() {
    const { getFieldDecorator } = this.props.form

return (
  <Form layout={'horizontal'}>
    <Form.Item>
      {getFieldDecorator('userName', {
        rules: [{ required: true, message: 'Please input your username!' }]
      })(<Input prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />} placeholder="Username" />)}
    </Form.Item>
  </Form>
  )
 }
}

export const RegistrationForm = Form.create()(FormWrapper)

**** 导入组件 ****

import React, { Component } from 'react'

/** Components **/
import { RegistrationForm } from 'Components/RegistrationForm'

/** UI Framework Components **/
import { Card, Tabs } from 'antd'

/** Styled Components **/
const Wrapper = styled(Card)`
  ${center()};
  width: 500px;
`

const TabPane = Tabs.TabPane 

export class LoginRegisterContainer extends Component {
  state = {
    activeTab: '1'
  }

render() {
   const { activeTab } = this.state

return (
  <Wrapper>
    <Tabs defaultActiveKey={activeTab}>
      <TabPane tab="Register" key="1">
        <RegistrationForm />
      </TabPane>
      <TabPane tab="Log In" key="2">
        <LogIn />
      </TabPane>
    </Tabs>
  </Wrapper>
   )
  }
}

我可以毫无问题地从库中导入所有组件,但是当尝试使用表单时,就会出现这种情况。

【问题讨论】:

    标签: reactjs react-redux antd


    【解决方案1】:

    我没有足够的声誉来发表评论,但我认为问题只是 &lt;Wrapper /&gt; 组件不存在。这就是你想要的结果吗?

    【讨论】:

    • 抱歉,Wrapper 确实存在。它是一个样式化的组件。我意识到我忘了把它粘贴到代码示例中。
    • 如果我删除 Form.Item 并假设放置一个

      标签,它会呈现得非常好。

    • 不幸的是,这没有帮助。 styled() 在这里也未定义,看起来存在一些语法问题。我认为您的问题存在于此处提供的代码之外。
    【解决方案2】:

    首先我执行了你的代码,检查了错误,真的返回了现有的组件,可能你没有发布它,然后我调整了它,它可以运行了。

        import React, { Component } from 'react'
    
        /** Components **/
        import { RegistrationForm } from './components/RegistrationForm'
    
        /** UI Framework Components **/
        import { Tabs } from 'antd'
    
        const TabPane = Tabs.TabPane
    
        export default class LoginRegisterContainer extends Component {
          state = {
            activeTab: '1',
          }
    
          render () {
            const {activeTab} = this.state
    
            return (
              <div>
                <Tabs defaultActiveKey={activeTab}>
                  <TabPane tab="Register" key="1">
                    <RegistrationForm/>
                  </TabPane>
                  <TabPane tab="Log In" key="2">
                    {/*<LogIn/>*/}
                  </TabPane>
                </Tabs>
              </div>
            )
          }
        }
    

    【讨论】:

    • 点评来源: 您好,请不要只回答源代码。尝试对您的解决方案如何工作提供一个很好的描述。请参阅:How do I write a good answer?。谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2021-03-25
    • 2020-06-14
    • 2020-02-29
    • 1970-01-01
    相关资源
    最近更新 更多