reactjs - 元素类型无效 : expected a string (for built-in components) or a class/function

标签 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();
<小时/>

我试图在 redux 的帮助下调用 API 并将其数据显示在表格中。我收到这个错误。上面是我的 index.js 文件。

<小时/>

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.

我引用了很多答案,但我无法识别错误。

最佳答案

问题出在导入语句中。您在导入一些组件(例如 createHistory)时没有使用大括号。

如果组件使用默认导出,例如:

export default X

然后你可以像这样简单地导入它:

import X from './X';

但是如果您使用的是命名导出,例如:

export const X=1;

然后您需要将其导入大括号中,例如:

import {X} from './X';

所以请查看您的导入。这将解决您的问题。

关于reactjs - 元素类型无效 : expected a string (for built-in components) or a class/function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44897070/

相关文章:

reactjs - 警告 : Failed prop type: The prop `store` is marked as required in `Provider` , 但其值为 `undefined` 。在提供者中

javascript - 使用 ESLint react PropType

html - 使用 Chrome 调试 React TypeScript .tsx 文件 - Webpack

javascript - React onChange 方法不改变对象的状态

javascript - 在内容安全策略中使用随机数或哈希值作为内联样式

javascript - React Redux Router 不路由

javascript - 加密,然后将访问 token 存储在 localStorage 中

javascript - 根据 Redux 操作负载从数组中删除项目

javascript - 类型 '{ state: any; dispatch: React.Dispatch<{ type: string; value: any; }>; }' 不可分配给类型

javascript - redux-form:访问handleSubmit 上的状态和表单值