I have a parent Component as 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
and there is a child component as 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
It is throwing an error 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 and
Error: A may have only one child element
I'm just starting with react so it might be a noob question but I am struck up here, If I pass <Scholarships />
directly into main.js it is working as expected why I am not able to nest it here
Additionally I've also tried
const scholarship = () => <Scholarships />
const scholarship = <Scholarships />
const scholarship = () => (<Scholarships />)
const scholarship = () => {Scholarships}
I would also like to know that my Scholarship component is return plain JSON.Stringify text so why is it still a Object in reference of the error given by const scholarship = () => <Scholarship />
My main.js file
import React from 'react'
import ReactDOM from 'react-dom'
import App from './app'
import Scholarships from './scholarships'
ReactDOM.render(<App />, document.getElementById('root'))