尝试得到react-router (v4.0.0)和react-hot loader (3.0.0-beta.6)很好地发挥,但在浏览器控制台得到以下错误:
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.
index.js:
import React from 'react';
import ReactDom from 'react-dom';
import routes from './routes.js';
require('jquery');
import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap/dist/js/bootstrap.min.js';
import './css/main.css';
const renderApp = (appRoutes) => {
ReactDom.render(appRoutes, document.getElementById('root'));
};
renderApp( routes() );
routes.js:
import React from 'react';
import { AppContainer } from 'react-hot-loader';
import { Router, Route, browserHistory, IndexRoute } from 'react-router';
import store from './store/store.js';
import { Provider } from 'react-redux';
import App from './containers/App.jsx';
import Products from './containers/shop/Products.jsx';
import Basket from './containers/shop/Basket.jsx';
const routes = () => (
<AppContainer>
<Provider store={store}>
<Router history={browserHistory}>
<Route path="/" component={App}>
<IndexRoute component={Products} />
<Route path="/basket" component={Basket} />
</Route>
</Router>
</Provider>
</AppContainer>
);
export default routes;
EDIT
你把过程复杂化了。只要这样做:
index.js:
import React from 'react';
import ReactDom from 'react-dom';
import routes from './routes.js';
require('jquery');
import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap/dist/js/bootstrap.min.js';
import './css/main.css';
ReactDom.render(<routes />, document.getElementById('root'));
routes.js:
import React from 'react';
import { AppContainer } from 'react-hot-loader';
import { Router, Route, browserHistory, IndexRoute } from 'react-router';
import store from './store/store.js';
import { Provider } from 'react-redux';
import App from './containers/App.jsx';
import Products from './containers/shop/Products.jsx';
import Basket from './containers/shop/Basket.jsx';
const routes =
<AppContainer>
<Provider store={store}>
<Router history={browserHistory}>
<Route path="/" component={App}>
<IndexRoute component={Products} />
<Route path="/basket" component={Basket} />
</Route>
</Router>
</Provider>
</AppContainer>;
export default routes;
大多数情况下,这是由于不正确的导出/导入。
常见错误:
// File: LeComponent.js
export class LeComponent extends React.Component { ... }
// File: App.js
import LeComponent from './LeComponent';
可能的选项:
// File: LeComponent.js
export default class LeComponent extends React.Component { ... }
// File: App.js
import LeComponent from './LeComponent';
有几种方法可能是错误的,但这种错误是由于导入/导出不匹配造成的,每次都是如此。
Edit
通常情况下,您应该得到一个堆栈跟踪,它指示故障发生的大致位置。这通常跟在你最初问题的信息之后。
如果没有显示,可能需要调查原因(可能是您遗漏了一个构建设置)。无论如何,如果它没有显示,唯一的操作方法是缩小导出/导入失败的地方。
遗憾的是,在没有stacktrace的情况下,唯一的方法是手动删除每个模块/子模块,直到您不再得到错误,然后再从堆栈中返回。
编辑2
通过注释,这确实是一个导入问题,特别是导入一个不存在的模块
我所缺少的是我正在使用
import { Router, Route, browserHistory, IndexRoute } from 'react-router';
正确答案应该是:
import { BrowserRouter as Router, Route } from 'react-router-dom';
当然你需要添加npm包react-router-dom:
npm install react-router-dom@next --save
当我将css文件添加到与组件文件相同的文件夹时,我遇到了这个问题。
我的重要陈述是:
import MyComponent from '../MyComponent'
当只有一个文件MyComponent.jsx时,这很好。(我在一个例子中看到了这种格式,并尝试了一下,然后忘记了我已经做到了)
当我添加MyComponent时。将SCSS导入到同一文件夹,则导入失败。也许JavaScript反而加载了.scss文件,所以没有出现错误。
我的结论是:即使只有一个文件,也要指定文件扩展名,以防稍后添加另一个文件。
这是一个在某种程度上必须调试的错误。正如已经说过很多次,不恰当的导入/导出会导致这个错误,但令人惊讶的是,我从我的react-router-dom认证设置中的一个小错误中得到了这个错误,下面是我的情况:
错误的设置:
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={(props) => (token ? <Component {...props} /> : <Redirect to={{ pathname: "/login" }} />)}
/>
);
正确设置:
const PrivateRoute = ({ component: Component, token, ...rest }) => (
<Route
{...rest}
render={(props) => (token ? <Component {...props} /> : <Redirect to={{ pathname: "/login" }} />)}
/>
);
唯一的区别是我在PrivateRoute组件中解构了令牌。顺便说一下,令牌是从localstorage获取的,像这样const token = localstorage . getitem ("authUser");所以如果它不在那里,我知道用户没有验证。这也会导致错误。
组件数组
获得此错误的常见方法是使用组件数组,并使用位置索引从数组中选择要呈现的组件。我多次看到这样的代码:
const checkoutSteps = [Address, Shipment, Payment]
export const Checkout = ({step}) => {
const ToRender = checkoutSteps[step]
return (
<ToRender />
)
}
这不是必要的坏代码,但如果你用一个错误的索引(例如-1,或者在这种情况下是3)调用它,ToRender组件将是未定义的,抛出React。createElement:类型无效…错误:
<Checkout step={0} /> // <Address />
<Checkout step={1} /> // <Shipment />
<Checkout step={2} /> // <Payment />
<Checkout step={3} /> // undefined
<Checkout step={-1} /> // undefined
合理的解决方案
你应该使用更明确的方法保护自己和同事不受这些难以调试的代码的影响,避免使用神奇的数字并使用PropTypes:
const checkoutSteps = {
address: Address,
shipment Shipment,
payment: Payment
}
const propTypes = {
step: PropTypes.oneOf(['address', 'shipment', 'payment']),
}
/* TIP: easier to maintain
const propTypes = {
step: PropTypes.oneOf(Object.keys(checkoutSteps)),
}
*/
const Checkout = ({step}) => {
const ToRender = checkoutSteps[step]
return (
<ToRender />
)
}
Checkout.propTypes = propTypes
export default Checkout
你的代码看起来是这样的:
// OK
<Checkout step="address" /> // <Address />
<Checkout step="shipment" /> // <Shipment />
<Checkout step="payment" /> // <Payment />
// Errors
<Checkout step="wrongstep" /> // explicit error "step must be one of..."
<Checkout step={3} /> // explicit error (same as above)
<Checkout step={myWrongVar} /> // explicit error (same as above)
这种方法的好处
代码更明确,你可以清楚地看到你想呈现什么
你不需要记住数字和它们隐藏的含义(1代表地址,2代表…)
错误也是显式的
不会让你的同事头疼:)
// @flow
import React from 'react';
import { styleLocal } from './styles';
import {
View,
Text,
TextInput,
Image,
} from 'react-native';
import { TouchableOpacity } from 'react-native-gesture-handler';
export default React.forwardRef((props, ref) => {
const { onSeachClick, onChangeTextSearch , ...otherProps } = props;
return (
<View style={styleLocal.sectionStyle}>
<TouchableOpacity onPress={onSeachClick}>
<Image
source={require('../../assets/imgs/search.png')}
style={styleLocal.imageStyle} />
</TouchableOpacity>
<TextInput
style={{ flex: 1, fontSize: 18 }}
placeholder="Search Here"
underlineColorAndroid="transparent"
onChangeText={(text) => { onChangeTextSearch(text) }}
/>
</View>
);
});
import IGPSSearch from '../../components/IGPSSearch';
<Search onSeachClick={onSeachClick} onChangeTextSearch= {onChangeTextSearch}> </Search>
在我的例子中,我添加了相同的自定义组件作为道具,而不是实际的道具
import AddAddress from '../../components/Manager/AddAddress';
class Add extends React.PureComponent {
render() {
const {
history,
addressFormData,
formErrors,
addressChange,
addAddress,
isDefault,
defaultChange
} = this.props;
错误:
return (
<AddAddress
addressFormData={addressFormData}
formErrors={formErrors}
addressChange={addressChange}
addAddress={AddAddress} // here i have used the Component name as probs in same component itself instead of prob
isDefault={isDefault}
defaultChange={defaultChange}
/>
</SubPage>
解决方案:
return (
<AddAddress
addressFormData={addressFormData}
formErrors={formErrors}
addressChange={addressChange}
addAddress={addAddress} // removed the component name and give prob name
isDefault={isDefault}
defaultChange={defaultChange}
/>
</SubPage>
所以我遇到了这个问题,我用一种奇怪的方式解决了它。我想我应该把它扔了,以防其他人遇到它,变得绝望。
我有一个有条件地呈现react元素,它只在对象状态大于零时才会呈现。在呈现函数中-当三元运算符被证明为真时,我收到了这篇文章所涉及的错误。我使用了一个<React。片段>在我的渲染函数-这是我通常分组子的方式,而不是更新的方式与更新的短语法(<>和</>)。
However, when I changed the <React.Fragment> to the short syntax - the react element rendered correctly and the error stopped appearing. I took a look at Facebooks React-Fragment documentation, and I don't see any reason on there to believe there is a dramatic difference between the two that would cause this. Regardless, below are two simplified snippets of code for your review. The first one demonstrates the code that was yielding the error, and the second one demonstrates the code that fixed the issue. At the bottom is the render function of the component itself.
希望有人觉得这有帮助。
代码示例1 -错误
有条件渲染的React元素
renderTimeFields = () => {
return (
<React.Fragment>
<div className="align-left">
<div className="input-row">
<label>Date: </label>
<input type="date" className="date" name="date" min={new Date().toISOString().split('T')[0]} required/>
</div>
</div>
<div className="align-left sendOn-row">
<div className="input-row">
<label>Time: </label>
<input type="time" className="time" name="time" required/>
</div>
</div>
</React.Fragment>
);
}
代码示例2 -无错误
有条件渲染的React元素
renderTimeFields = () => {
return (
<>
<div className="align-left">
<div className="input-row">
<label>Date: </label>
<input type="date" className="date" name="date" min={new Date().toISOString().split('T')[0]} required/>
</div>
</div>
<div className="align-left sendOn-row">
<div className="input-row">
<label>Time: </label>
<input type="time" className="time" name="time" required/>
</div>
</div>
</>
);
}
在组件中呈现函数
render() {
<form>
...
{emailsInActiveScript > 1 ? this.renderTimeFields() : null}
</form>
etc...
}
就我而言,VS Code让我失望了。
这是我的组件的层次结构:
<HomeScreen> => <ProductItemComponent> => <BadgeProductComponent>
我错误地导入了ProductItemComponent。事实上,这个组件以前在共享文件夹中,但后来它被移动到主文件夹中。但是当我将文件移动到另一个文件夹时,导入没有更新,保持不变:
../shared/components
同时,组件工作正常,VS Code没有突出显示错误。但是当我添加了一个新的BadgeProductComponent到ProductItemComponent,我有一个渲染错误,并认为问题是在新的BadgeProductComponent,因为当这个组件被删除,一切工作!
更重要的是,如果我通过热键到ProductItemComponent它有。/shared/components地址,然后VS Code重定向到Home文件夹的地址../ Home /components。
通常,检查所有组件级别上所有导入的正确性。
我已经遇到过这个问题了。我的解决方案是:
在文件配置路由:
const routes = [
{ path: '/', title: '', component: Home },
{ path: '*', title: '', component: NotFound }
]
to:
const routes = [
{ path: '/', title: '', component: <Home /> },
{ path: '*', title: '', component: <NotFound /> }
]
对于我来说,这发生在我试图将命名导入作为默认导入时,所以我得到了这个错误
import ProductCard from '../../../components/ProductCard' // that what caused the issue
Error: 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, or you might have mixed up default and named imports.
Check the render method of `TopVente`.
所以我必须通过名称导入来修复它
import { ProductCard } from '../../../components/ProductCard'