question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid element type

See original GitHub issue

💬 Questions and Help

I am currently adding loadable-components in an SSR project. I got the below warning and the lazy component is not loaded

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: symbol.

Check the render method of `RouterContext`.
    in RouterContext (created by Router)
    in Router
    in Provider
    in HelmetProvider
    in ApolloProvider
    in ErrorBoundary

I suspect it is the same reason as #310 because I am currently using react-router v3, but not sure if there is a workaround for so?

Below is my route config

{
    path: 'about(/)',
    getComponent: loadable(() => import('./containers/AboutPage')),
},

Client babel config:

{
  presets: [
    [
      '@babel/preset-env',
      {
        'targets': {
          browsers: [
            'last 2 versions',
            'safari >= 7',
            'ie >= 9',
          ],
        },
        'useBuiltIns': 'usage',
        'corejs': '3.6',
        'modules': false,
      },
    ],
    '@babel/preset-react',
    '@babel/preset-flow',
  ],
  plugins: [
    '@babel/plugin-proposal-class-properties',
    '@babel/plugin-proposal-optional-chaining',
    '@emotion',
    '@loadable',
  ]
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
tomtom94commented, Apr 1, 2021

routes.js file below

import baseLoadable, { LoadableComponent } from '@loadable/component'
import React from 'react'
import Loading from './Exception/Loading'

// https://github.com/gregberge/loadable-components/issues/669#issuecomment-741539840
const loadable = importer => {
  const withoutForwardRef = process.env.BROWSER ? C => props => <C {...props} /> : C => C
  return withoutForwardRef(
    baseLoadable(importer, {
      fallback: <Loading />
    })
  )
}

const Forbidden = loadable(() => import(/* webpackPrefetch: true, webpackChunkName: "Forbidden" */ './Exception/403'))
const NoMatch = loadable(() => import(/* webpackPrefetch: true, webpackChunkName: "NoMatch" */ './Exception/404'))
const ServerDown = loadable(() => import(/* webpackPrefetch: true, webpackChunkName: "ServerDown" */ './Exception/500'))

const DailyResults = loadable(() => import(/* webpackPrefetch: true, webpackChunkName: "DailyResults" */ './DailyResults/index'))
const FullSeasons = loadable(() => import(/* webpackPrefetch: true, webpackChunkName: "FullSeasons" */ './FullSeasons/index'))
const HeadToHead = loadable(() => import(/* webpackPrefetch: true, webpackChunkName: "HeadToHead" */ './HeadToHead/index'))
const Seasons = loadable(() => import(/* webpackPrefetch: true, webpackChunkName: "Seasons" */ './Seasons/index'))
const TeamResults = loadable(() => import(/* webpackPrefetch: true, webpackChunkName: "TeamResults" */ './TeamResults/index'))
const Connection = loadable(() => import(/* webpackPrefetch: true, webpackChunkName: "Connection" */ './Connection/index'))
const LostPassword = loadable(() => import(/* webpackPrefetch: true, webpackChunkName: "LostPassword" */ './Connection/LostPassword'))
const SignUp = loadable(() => import(/* webpackPrefetch: true, webpackChunkName: "SignUp" */ './Connection/SignUp'))
const User = loadable(() => import(/* webpackPrefetch: true, webpackChunkName: "User" */ './User/index'))
const CurrentSeasons = loadable(() => import(/* webpackPrefetch: true, webpackChunkName: "CurrentSeasons" */ './CurrentSeasons'))

const AboutUs = loadable(() => import(/* webpackPrefetch: true, webpackChunkName: "AboutUs" */ './AboutUs'))
const PrivacyPolicy = loadable(() => import(/* webpackPrefetch: true, webpackChunkName: "PrivacyPolicy" */ './PrivacyPolicy'))
const Terms = loadable(() => import(/* webpackPrefetch: true, webpackChunkName: "Terms" */ './Terms'))

// export interface Route {
//   name?: string
//   exact?: boolean
//   path?: string | string[]
//   // eslint-disable-next-line @typescript-eslint/no-explicit-any
//   Component: LoadableComponent<any>
// }

const indexRoutes = [
  {
    path: '/seasons/:type(league-tables|national-cups|international-cups)',
    Component: FullSeasons
  },
  {
    path: '/:type(league-tables|national-cups|international-cups)',
    Component: CurrentSeasons
  },
  {
    path: '/:type(league-table|tournament)/:seasonId',
    Component: Seasons
  },
  {
    exact: true,
    path: ['/', '/daily-results/:date([0-9]{4}-[0-9]{2}-[0-9]{2})?'],
    Component: DailyResults
  },
  {
    path: '/head-to-head/:fixtureId',
    Component: HeadToHead
  },
  {
    path: '/connection',
    Component: Connection
  },
  {
    path: '/lost-password',
    Component: LostPassword
  },
  {
    path: '/sign-up',
    Component: SignUp
  },
  {
    path: '/user/:nickname',
    Component: User
  },
  {
    path: '/team-results/:teamId',
    Component: TeamResults
  },
  {
    path: '/loading',
    Component: Loading
  },
  {
    path: '/about-us',
    Component: AboutUs
  },
  {
    path: '/privacy-policy',
    Component: PrivacyPolicy
  },
  {
    path: '/terms',
    Component: Terms
  },
  { name: 'NoMatch', Component: NoMatch }
]

export default indexRoutes

But when I change file extension from routes.js to route.ts it doesn’t work.

BUT you are right routes.tsx works VERY FINE !!! many thanks

0reactions
tomtom94commented, Oct 16, 2022

@theKashey @IniZio Do we still have to use this workaround ?

const loadable = importer => {
  const withoutForwardRef = process.env.BROWSER ? C => props => <C {...props} /> : C => C
  return withoutForwardRef(
    baseLoadable(importer, {
      fallback: <Loading />
    })
  )
}

I am on react 17 and react-router 5 Because I have been trying without and it’s all good. Can you confirm please ? Cheers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invariant Violation: Element type is invalid: expected a string ...
This error can rise if you try to import a non-existent component. Make sure you have no typo and that the component indeed...
Read more >
(React) Element type is invalid, expected a string (for built in ...
To solve the error "Element type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got", ......
Read more >
Element type is invalid: expected a string (for built-in ... - GitHub
The app breaks and gives this error message: Element type is invalid: expected a string (for built-in components) or a class/function (for ...
Read more >
Element type is invalid: expected a string (for built-in ... - Reddit
Getting Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: ...
Read more >
Server Error Error: Element type is invalid: expected a string ...
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found