[reactjs] Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: object

I was having the same issue and realized that I was providing an Undefined React component in the JSX markup. The thing is that the react component to render was dynamically calculated and it ended up being undefined!

The error stated:

Invariant Violation: 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 C.,

The example producing this error:

_x000D_
_x000D_
var componentA = require('./components/A');_x000D_
var componentB = require('./components/B');_x000D_
_x000D_
const templates = {_x000D_
  a_type: componentA,_x000D_
  b_type: componentB_x000D_
}_x000D_
_x000D_
class C extends React.Component {_x000D_
  constructor(props) {_x000D_
    super(props);_x000D_
  }_x000D_
  _x000D_
  // ..._x000D_
  _x000D_
  render() {_x000D_
    //Let´s say that depending on the uiType coming in a data field you end up rendering a component A or B (as part of C)_x000D_
    const ComponentTemplate = templates[this.props.data.uiType];_x000D_
    return <ComponentTemplate></ComponentTemplate>_x000D_
  }_x000D_
  _x000D_
  // ..._x000D_
}
_x000D_
_x000D_
_x000D_

The problem was that an invalid "uiType" was provided and therefore this was producing undefined as result:

_x000D_
_x000D_
templates['InvalidString']
_x000D_
_x000D_
_x000D_

Examples related to reactjs

Error: Node Sass version 5.0.0 is incompatible with ^4.0.0 TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined raised when starting react app Template not provided using create-react-app How to resolve the error on 'react-native start' Element implicitly has an 'any' type because expression of type 'string' can't be used to index Invalid hook call. Hooks can only be called inside of the body of a function component How to style components using makeStyles and still have lifecycle methods in Material UI? React Hook "useState" is called in function "app" which is neither a React function component or a custom React Hook function How to fix missing dependency warning when using useEffect React Hook? Unable to load script.Make sure you are either running a Metro server or that your bundle 'index.android.bundle' is packaged correctly for release

Examples related to react-router

React : difference between <Route exact path="/" /> and <Route path="/" /> You should not use <Link> outside a <Router> React Router Pass Param to Component Detect Route Change with react-router What is the best way to redirect a page using React Router? No restricted globals React-router v4 this.props.history.push(...) not working How to pass params with history.push/Link/Redirect in react-router v4? How to use Redirect in the new react-router-dom of Reactjs How to implement authenticated routes in React Router 4?