Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element type is invalid: expected a string (for built-in components) or a class/function

Tags:

reactjs

redux

import React from 'react'; import ReactDOM from 'react-dom'; import Map from './components/map/container/map'; import App from './App'; import './index.css'; import shell from './shared/utility/shell'; import { render } from 'react-dom' import { Provider } from 'react-redux' import { createStore, applyMiddleware, compose } from 'redux' import { routerMiddleware, ConnectedRouter } from 'react-router-redux' import thunk from 'redux-thunk' import createHistory from 'history/createBrowserHistory' import rootReducer from './reducers' import registerServiceWorker from './registerServiceWorker'; import { Routes } from './config';  const history = createHistory(); const target = document.querySelector('#root') const initialState = {}; const enhancers = []; const middleware = [ thunk, routerMiddleware(history)  ];  if (process.env.NODE_ENV === 'development') { const devToolsExtension = window.devToolsExtension;  if (typeof devToolsExtension === 'function') { enhancers.push(devToolsExtension());  } } const composedEnhancers = compose( applyMiddleware(...middleware),   ...enhancers ); const store = createStore(  rootReducer,  initialState,  composedEnhancers ); render(   <Provider store={store}>    <ConnectedRouter history={history}>     <div>      <Routes />     </div>    </ConnectedRouter>   </Provider>,   target    )  registerServiceWorker();  ReactDOM.render(<App />, document.getElementById('root'));  registerServiceWorker(); 

I am trying to call an API from with the help of redux and display its data in a table. I am getting this error. Above is my index.js file.


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


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

I have referred to many answers I am not able to recognize the error.

like image 441
Varun thadhani Avatar asked Jul 04 '17 04:07

Varun thadhani


People also ask

How do you fix element type is invalid expected a string for built-in components or a class function for composite components but got undefined?

To solve the error "Element type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got", make sure to import named exports using curly braces and default exports without, and only use functions or classes as components.

How do you pass components as props react?

You can pass a component as props in React by using the built-in children prop. All elements you pass between the opening and closing tags of a component get assigned to the children prop.

What does react cloneElement do?

React. cloneElement() is part of the React Top-Level API used to manipulate elements. It clones and returns a new element using its first argument as the starting point. This argument can be a React element or a component that renders a React element.

What is 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.


2 Answers

Problem is in the import statements. You have not used curly braces while importing some components like createHistory.

If a component uses a default export like:

export default X 

Then you can simply import it like:

import X from './X'; 

But if you are using a named export like:

export const X=1; 

Then you need to import it in curly braces like:

import {X} from './X'; 

So have a look at your imports. This will resolve your issue.

like image 183
Vikram Saini Avatar answered Oct 19 '22 03:10

Vikram Saini


I ran into the same problem in my react application. It's definitely your import statements as stated above. I changed:

import {x} from '/x.js' to import x from '/x.js'

and that got the job done

like image 33
Olumide Avatar answered Oct 19 '22 05:10

Olumide