当前位置: 首页 > 知识库问答 >
问题:

React.create元素:类型无效-预期一个字符串(用于内置组件)或一个类/函数,但得到:未定义的[重复]

茅才
2023-03-14

我有一个父组件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无效--应为字符串(对于内置组件)或类/函数(对于复合组件),但Get:undefined。您可能忘记了从定义组件的文件中导出组件。检查App的渲染方法。bundle.js:357:9和Error:A可能只有一个子元素,我刚从react开始,所以这可能是一个noob问题,但如果我通过

此外,我也试过

  • const奖学金=()=

我还想知道我的奖学金组件是返回纯JSON. Stringify文本,那么为什么它仍然是引用const奖学金的错误的对象 = () =

我的main.js档案

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

共有1个答案

羊丰茂
2023-03-14

这样写:

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

原因:React组件不能返回多个React元素,但单个JSX表达式可以有多个子元素,因此如果希望组件呈现多个内容,可以将它们包装在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')
)
 类似资料: