index.js
import React from "react";
import ReactDOM from "react-dom";
import App from "/src/App.jsx";
import { BrowserRouter } from "react-router-dom";
ReactDOM.render(
<>
<BrowserRouter>
<App />
</BrowserRouter>
</>,
document.getElementById("root")
);
App.jsx
import React from "react";
import { Switch, Route } from "react-router-dom";
import Home from "/src/Home.jsx";
function App() {
return (
<>
<Switch>
<Route exact path="/" component={Home} />
</Switch>
</>
);
}
export default App;
Here is the codesandbox:
https://codesandbox.io/s/webapp-3tw58
Getting an error, not understanding how to resolve it
Element 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 the render method of App.
You've done some mistake over there, regarding import and all that.
First thing is you can't import with file extension.
import App from "/src/App.jsx";
instead
import App from "/src/App";
and same for App.jsx file as well.
import Home from "/src/Home";
OR
I'll suggest to use the relative import,
import App from "./App";
Maybe this is causing the actual issue.
Updated:
By looking into your codesandbox, you're using the react-router-dom#v6 and there is a different way of using the Route. Please read it from here.
As mentioned by #Pradip Dhakal, this is likely an issue caused by react-router-dom version mismatch. Updating route definition as per new documentation seems to solve the issue:
App.jsx
import { BrowserRouter, Routes, Route } from "react-router-dom";
import Home from "./Home";
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
</Routes>
</BrowserRouter>
);
}
export default App;
Home.jsx
import React from "react";
function Home() {
return <div>Home</div>;
}
export default Home;
Related
I'm working with my colleague on the project and everything works on his PC, but not on mine. I got this bug:
Error: Element 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.
It seems to be a problem with import/export but I can't find this.
Can you please help me?
File index.tsx:
import React from 'react';
import ReactDOM from 'react-dom';
import { App } from './App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root'),
);
File app.tsx:
import React from 'react';
import DocumentTitle from 'react-document-title';
import { AppMainWrapper } from './modules/AppMainWrapper/AppMainWrapper.component';
export const App: React.FC = () => {
return (
<DocumentTitle title="Finn den rĂ¥este roomie med oss">
<AppMainWrapper />
</DocumentTitle>
);
};
AppMainWrapperComponent.tsx
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import { getBaseUrl, routes } from '../../routes/routes';
import { Footer } from '../components/Footer/Footer.component';
import { Home } from './components/Home/Home.component';
import { Login } from './components/Login/Login.component';
import { NewUser } from './components/NewUser/NewUser.component';
export const AppMainWrapper: React.FC = () => {
const { main } = routes;
return (
<>
<Router>
<Switch>
<Route
path={getBaseUrl()}
exact
component={Home}
/>
<Route
path={main.login.path}
component={Login}
/>
<Route
path={main.newUser.path}
component={NewUser}
/>
</Switch>
</Router>
<Footer />
</>
);
};
I have no idea what can be wrong here. Thanks!
i am working using BrowserRouter.Router from react-router-dom in my App.js to link all my pages.
When after using react router, i get this error in the browser.
enter image description here
As you can see, the error is pointing to reactDOM.render in my index.js. I don't understand why that should be an issue if i use react router in App.js.
This is that my App.js looks like:
import React from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import Navbar from './components/Navbar/index';
import Footer from './components/Footer/index';
//pages
import Home from "./components/Pages/Home";
// import About from "./components/Pages/About";
import Portfolio from "./components/Pages/Portfolio";
import Contact from "./components/Pages/Contact";
import './App.css';
function App() {
return(
<Router>
<Navbar/>
<Route exact path="/" component={Home}/>
<Route exact path="/about" component={Home}/>
<Route exact path="/portfolio" component={Portfolio}/>
<Route exact path="/contact" component={Contact}/>
<Footer/>
</Router>
);
}
export default App;
This is what my index.js file looks like:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers:
serviceWorker.unregister();
I'm not sure what the issue is, could someone explain to me why reactDOM.render is affected by using Router in App.js. It looks like i've imported everything properly. I need a second pair of eyes. PLEASE HELP! lol Thank you
CodeSandbox located:
https://codesandbox.io/s/vigorous-colden-0c73r?file=/src/App.js
I'm creating a simple React website and my console keeps showing this error when I try to open the home page in the browser.
"Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports."
After looking at other questions from people who had the same issue, it seems they were lacking curly braces in the imports, but I believe I am doing that correctly. In fact, I cannot find anything wrong with my imports/exports. Any direction will be greatly appreciated! Also, I am using mapDispatch because I am also using Redux here.
My component:
import React from 'react';
import { HashRouter as Router, Route, Switch } from 'react-router-dom';
import { connect } from 'react-redux';
import { getCampuses, getStudents } from '../reducers';
import Nav from './Nav';
import Home from './Home';
import Students from './Students';
import Campuses from './Campuses';
import EditCampus from './EditCampus';
import EditStudent from './EditStudent';
import CreateStudent from './CreateStudent';
import CreateCampus from './CreateCampus';
import Student from './Student';
import Campus from './Campus';
import Footer from './Footer';
class Root extends React.Component{
componentDidMount() {
this.props.fetch();
}
render() {
return (
<Router>
<div>
<Nav />
<div className='container-fluid'>
<Switch>
<Route path='/students/:id' component={ Student } />
<Route path='/campuses/:id' component={ Campus } />
<Route path='/editstudent/:id' component={ EditStudent } />
<Route path='/editcampus/:id' component={ EditCampus } />
<Route path='/createstudent' component={ CreateStudent } />
<Route path='/createcampus' component= { CreateCampus } />
<Route path='/students' component={ Students } />
<Route path='/campuses' component={ Campuses } />
<Route exact path='/' component={ Home } />
</Switch>
</div>
<Footer />
</div>
</Router>
);
}
}
const mapState = null;
const mapDispatch = dispatch => ({
fetch() {
dispatch(getCampuses());
dispatch(getStudents());
}
});
export default connect(mapState, mapDispatch)(Root);
Rendering the DOM:
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import store from './store';
import Root from './components/Root';
ReactDOM.render(
<Provider store={store}>
<Root />
</Provider>,
document.getElementById('main')
)
Ok try this you are using HashRouter which is used for the static website but in your case, it is not. So try this,
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
So when I changed to BrowserRouter I got a different error that said "Uncaught TypeError: (0 , _store.getCampuses) is not a function". I finally got it to work though when I imported the getCampuses function more specifically with '../reducers/campuses' (where I was exporting the getCampuses function). I did the same with getStudents ('../reducers/students'). Thank you for pointing me in the right direction!
i recently tried to make separate files .jsx in React. I made couple of them with export default name / import name from ./name.jsx'. But there comes problem. First i had imported Route and Links from react-router and console said it can't find Links, i found on stackoverflow to change it to react-router-dom, so i did it, and now console says i forgot to export some components. I can't find solution :( Could anyone help me ?
Here's my project :
https://github.com/tafarian/Portfolio
import React from 'react';
import ReactDOM from 'react-dom';
import './../css/style.scss';
import {
Router,
Route,
Link,
IndexLink,
IndexRoute,
hashHistory
} from 'react-router-dom';
import Template from './Template.jsx'
import Projects from './Projects.jsx'
import Home from './Home.jsx'
import AboutMe from './AboutMe.jsx'
import Contact from './Contact.jsx'
Such error as
"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.
means that you are trying to create an undefined component, and as it said in error - usually this happens when you forget to export component or the component that you are importing does not exist.
When there is a lot of components and it's hard to find which one is "bad" I place a breakpoint at console.error(message); in React code and go up the stacktrace to the function createElement and it's arguments. Usually it helps me to identify the problem.
The main problem, is that your code is not compatible with react-router#4.
In this version you can not pass children and component to Route at the same time.
Also, there is no such a thing as IndexRoute and you should use BrowserRouter instead of Router, or you should pass an history object.
Take a look at the documention: https://reacttraining.com/react-router/web/guides/philosophy
And here is fixed version of yout app.jsx:
import React from 'react';
import ReactDOM from 'react-dom';
import './../css/style.scss';
import {
BrowserRouter,
Route,
Link,
hashHistory
} from 'react-router-dom';
import Template from './Template.jsx'
import Projects from './Projects.jsx'
import Home from './Home.jsx'
import AboutMe from './AboutMe.jsx'
import Contact from './Contact.jsx'
document.addEventListener('DOMContentLoaded', function(){
class App extends React.Component {
state = {
loading: true,
};
componentDidMount() {
setTimeout(() =>
this.setState({
loading: false
}), 2500);
}
render() {
if(this.state.loading === true) {
return (
<div id="preloader">
<div id="loader"></div>
</div>
)
}
return(
<BrowserRouter history={hashHistory}>
<Template>
<Route exact path="/" component={Home} />
<Route path='/aboutme' component={AboutMe} />
<Route path='/projects' component={Projects} />
<Route path='/contact' component={Contact} />
</Template>
</BrowserRouter>
)
}
}
ReactDOM.render(
<App />,
document.getElementById('app')
);
});
While rewriting some components in es6 syntax my React router started throwing the following error:
Uncaught Error: Element 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 `Navicon`.
This error only springs if I include a <Link> component inside my Navicon component, which looks like this:
import React from 'react';
import Link from 'react-router';
import './navicon.css';
class Navicon extends React.Component {
constructor(props) {
super(props)
}
render() {
return (
<div className='navicon-container' tabIndex='0'>
<div className={'dropdown'}>
<Link to={'/'}>Hello</Link>
</div>
</div>
)
}
}
export default Navicon;
The component that mounts my router looks like this:
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, browserHistory, IndexRoute } from 'react-router';
import App from './components/App';
import Home from './components/Home';
import About from './components/About';
import './main.css';
ReactDOM.render(
<Router history={browserHistory}>
<Route component={App}>
<Route path='/' component={Home} />
<Route path='/about' component={About} />
</Route>
</Router>,
document.getElementById('app')
);
I'm using "react-router": "^3.0.2"
Does anyone have any idea what might be tripping this error? This error never sprang when I was using React.createClass(), so I'm puzzled.
Ah, it seems that due to the export syntax on react router components, one evidently must use:
// import Link from react-router <- this doesn't work
import {Link} from react-router // <- this does
That little change resolves the error.