【问题标题】:React.createElement: type is invalid -- expected a string (for built-in components) or a class/function but got: undefined [duplicate]React.createElement:类型无效——需要一个字符串(用于内置组件)或一个类/函数,但得到:未定义 [重复]
【发布时间】:2017-08-30 23:26:58
【问题描述】:

我有一个父组件 app.js

import React, { Component } from 'react'
import { Route, Link, BrowserRouter, HashRouter} from 'react-router-dom'
//import BrowserHistory from 'react-router/lib/BrowserHistory'
import {Scholarships} from './scholarships'

class App extends Component {
        render() {
                return (
                        <div>
                        <HashRouter>
                            <div>
                                <Route exact path='/' component={Home} />
                                <Route path='/hello' component={Hello} />
                                <Route path='/scholarship' component={Scholarships} />

                            </div>
                        <Scholarships /> 
                        </HashRouter>
                        </div>

                )

        }
}

const Home = () => <h1> Hello from home! </h1>
const Hello = () => <h2> Hello React </h2>

export default App

并且有一个子组件 scholarships.js

import React, {Component} from 'react'
import Request from 'react-http-request' //`https://www.npmjs.com/package/react-http-request`

class Scholarships extends Component {
        constructor(props) {
                super(props);
        }
        render() {
                return (
                        <Request
                                url = 'https://api.github.com/users/mbasso'
                                method = 'get'
                                accept = 'application/json'
                                verbose = {true}
                        >
                                {
                                        ({error, result, loading}) => {
                                                        if(loading) {
                                                                return <div id="scholarships"> loading... </div>;
                                                        }
                                                        else {
                                                                return <div id="scholarships"> { JSON.stringify(result) }</div> ;
                                                        }

                                                }
                                }
                        </Request>
                )
        }
}

export default Scholarships

它抛出一个错误 警告:React.createElement: type is invalid -- 期望一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义。您可能忘记从定义组件的文件中导出组件。检查App 的渲染方法。 bundle.js:357:9错误:A 可能只有一个子元素 我刚开始反应,所以这可能是一个菜鸟问题,但我在这里被打动了,如果我将&lt;Scholarships /&gt; 直接传递到 ma​​in.js 它按预期工作,为什么我不能嵌套在这里

另外我也试过了

  • const scholarship = () =&gt; &lt;Scholarships /&gt;
  • const scholarship = &lt;Scholarships /&gt;
  • const scholarship = () =&gt; (&lt;Scholarships /&gt;)
  • const scholarship = () =&gt; {Scholarships}

我还想知道我的 Scholarship 组件返回纯 JSON.Stringify 文本,那么为什么它仍然是引用 const scholarship = () =&gt; &lt;Scholarship /&gt; 给出的错误的 Object

我的 ma​​in.js 文件

import React from 'react'
import ReactDOM from 'react-dom'
import App from './app'
import Scholarships from './scholarships'
ReactDOM.render(<App />, document.getElementById('root'))

【问题讨论】:

  • 问题出在您的 App 组件的render()
  • @JamesDonnelly 你能详细说明一下吗?
  • 看重复的答案:stackoverflow.com/a/33385872/1317805.
  • @JamesDonnelly 我已经编辑了控制台抛出的问题和错误,我也已经尝试过这种方式,我只是不工作并给出不同的错误。

标签: javascript reactjs react-router react-router-dom


【解决方案1】:

这样写:

<HashRouter>
    <div>
        <Route exact path='/' component={Home} />
        <Route path='/hello' component={Hello} />
        <Route path='/scholarship' component={Scholarship} />
    </div>
    <Scholarships />
</HashRouter>

原因:一个React组件不能返回多个React元素,但是一个JSX表达式可以有多个子元素,所以如果你想让一个组件渲染多个东西你可以wrap他们在div

不要忘记render() 函数就是一个函数。函数总是接受多个参数并且总是只返回一个值。

更新

像这样渲染路线,它会起作用:

ReactDOM.render(
    <HashRouter>
        <div>
            <Route exact path='/' component={Home} />
            <Route path='/hello' component={Hello} />
            <Route path='/scholarship' component={Scholarships} />

        </div>
        <Scholarships /> 
    </HashRouter>,
    document.getElementbyId('root')
)

【讨论】:

  • 检查更新答案的原因。
  • 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. Check the render method of App. bundle.js:357:9 Error: A &lt;Router&gt; may have only one child element 更改后抛出这个
  • 你在ReactDOM.render(...)中渲染App ??
  • 检查更新的问题,也粘贴了 main.js
  • 检查更新的答案,直接在 ReactDOM.render 中渲染路由,它会起作用。原因我不知道我搜索了很多但没有找到原因。我尝试了很多方法,但这是它工作的唯一方法:) 我投票重新打开你的问题可能是有人提供了原因。
猜你喜欢
  • 1970-01-01
  • 2020-09-04
  • 1970-01-01
  • 2017-11-29
  • 1970-01-01
  • 2022-01-12
  • 2021-06-30
  • 2020-03-07
  • 1970-01-01
相关资源
最近更新 更多