Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<Link> tag producing error: React.createElement: type is invalid

Error in React Router's link tag while switching from 0.14 to v15.5. This worked fine in the former version and it throws the following error in the latter.

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 Navbar.

navbar.js

import React, { Component } from 'react';
import Link from 'react-router-dom';

class Navbar extends Component
{
  render()
  {
     return (

         <nav className="nav">                                         
           <Link className="btn btn-primary btn-block" to="/about">
           About Us
           </Link>
         </nav>

     );
  }

  export default Navbar;

index.js

import React from 'react';
import { render } from 'react-dom';
import { BrowserRouter, Route, Switch } from 'react-router-dom';

import HomePage from './components/homepage/homepage';
import AboutPage from './components/aboutpage/aboutpage';

render
(
    <BrowserRouter>
        <Switch>
            <Route path="/about" component={AboutPage} />
            <Route path="/" component={HomePage} />
        </Switch>
    </BrowserRouter>,
    document.getElementById('app')
);
like image 328
anonym Avatar asked Jul 03 '17 05:07

anonym


People also ask

What is createElement in React?

createElement() React. createElement( type, [props], [... children] ) Create and return a new React element of the given type. The type argument can be either a tag name string (such as 'div' or 'span' ), a React component type (a class or a function), or a React fragment type.

Why do React components need to be capitalized?

Specifying The React Element Type Capitalized types indicate that the JSX tag is referring to a React component. These tags get compiled into a direct reference to the named variable, so if you use the JSX <Foo /> expression, Foo must be in scope.

How JSX works in React?

JSX allows us to write HTML elements in JavaScript and place them in the DOM without any createElement() and/or appendChild() methods. JSX converts HTML tags into react elements. You are not required to use JSX, but JSX makes it easier to write React applications.

What is scope in React?

React Scope leverages React Developer Tools to retrieve information about the client's application. We then use this data to render a DOM tree visualization. The user simply hovers over the nodes within the tree to see each component's name, state, and props.


1 Answers

this error generally comes if you have not imported or exported a component successfully in your components.For more more info you can read react documentation of when to use curly braces while import and when not:)

import { Link } from "react-router-dom";

.

<ul>
  <li><Link to="x">x</Link></li>
  <li><Link to="y">y</Link></li>
  <li><Link to="z">z</Link></li>
</ul>
like image 172
Vikram Saini Avatar answered Sep 24 '22 10:09

Vikram Saini