Javascript react.js无状态组件给出;“无效”;导入时出错
当我尝试将无状态组件导入App.js以运行时,我收到一个错误Javascript react.js无状态组件给出;“无效”;导入时出错,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,当我尝试将无状态组件导入App.js以运行时,我收到一个错误type is invalid。当我在App.js中运行相同的代码时,它运行得很好,但当我导入它时,它就崩溃了。这里的解决方案是什么?发生了什么 先谢谢你 mymobile.js import React from 'react'; //this part will be imported via a component. Need to check and make sure how to update state via com
type is invalid
。当我在App.js中运行相同的代码时,它运行得很好,但当我导入它时,它就崩溃了。这里的解决方案是什么?发生了什么
先谢谢你
mymobile.js
import React from 'react';
//this part will be imported via a component. Need to check and make sure how to update state via component.
const checkIfMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (
checkIfMobile.Android() ||
checkIfMobile.BlackBerry() ||
checkIfMobile.iOS() ||
checkIfMobile.Opera() ||
checkIfMobile.Windows()
);
}
};
export default checkIfMobile;
它被导入到App.js中,并给我提供了类型无效
错误
import React, { Component } from 'react';
import ChromePluginNotice from '../components/banner/ChromePluginNotice';
import Content from './Content';
import Banner from './Banner';
import Footer from '../components/footer/Footer';
import checkIfMobile from '../components/banner/checks/mobile';
// //this part will be imported via a component. Need to check and make sure how to update state via component.
// const checkIfMobile = {
// Android: function() {
// return navigator.userAgent.match(/Android/i);
// },
// BlackBerry: function() {
// return navigator.userAgent.match(/BlackBerry/i);
// },
// iOS: function() {
// return navigator.userAgent.match(/iPhone|iPad|iPod/i);
// },
// Opera: function() {
// return navigator.userAgent.match(/Opera Mini/i);
// },
// Windows: function() {
// return navigator.userAgent.match(/IEMobile/i);
// },
// any: function() {
// return (
// checkIfMobile.Android() ||
// checkIfMobile.BlackBerry() ||
// checkIfMobile.iOS() ||
// checkIfMobile.Opera() ||
// checkIfMobile.Windows()
// );
// }
// };
export default class App extends Component {
constructor() {
super();
this.state = { isMobile: checkIfMobile.any() };
}
render() {
const { isMobile } = this.state; // destructure isMobile to variable
return (
<div>
<ChromePluginNotice />
<Banner isMobile={isMobile} />
<Content isMobile={isMobile} />
<Footer />
</div>
);
}
}
import React,{Component}来自'React';
从“../components/banner/ChromePluginNotice”导入ChromePluginNotice;
从“./Content”导入内容;
从“./Banner”导入横幅;
从“../components/Footer/Footer”导入页脚;
从“../components/banner/checks/mobile”导入checkIfMobile;
////此部件将通过组件导入。需要检查并确保如何通过组件更新状态。
//常量checkIfMobile={
//Android:function(){
//返回navigator.userAgent.match(/Android/i);
// },
//黑莓:函数(){
//返回navigator.userAgent.match(/BlackBerry/i);
// },
//iOS:function(){
//返回navigator.userAgent.match(/iPhone | iPad | iPod/i);
// },
//Opera:函数(){
//返回navigator.userAgent.match(/Opera-Mini/i);
// },
//Windows:function(){
//返回navigator.userAgent.match(/IEMobile/i);
// },
//any:function(){
//返回(
//checkIfMobile.Android()||
//checkIfMobile.BlackBerry()||
//checkIfMobile.iOS()||
//checkIfMobile.Opera()||
//checkIfMobile.Windows()
// );
// }
// };
导出默认类应用程序扩展组件{
构造函数(){
超级();
this.state={isMobile:checkIfMobile.any()};
}
render(){
const{isMobile}=this.state;//将isMobile分解为变量
返回(
);
}
}
不知道该怎么做才能从这里修复它。完全错误是
invariant.js?4599:44未捕获错误:元素类型无效:需要字符串(对于内置组件)或类/函数(对于复合组件),但得到:对象。检查
无状态组件的呈现方法
这是一些恶意代码试图访问旧版本的my mobile.js,现在一切正常。你能包含完整错误吗?更新了我的线程,但这里它是不变的.js?4599:44未捕获错误:元素类型无效:需要字符串(对于内置组件)或类/函数(对于复合组件),但得到:object。请检查
无状态组件的呈现方法
我在这里没有看到任何无状态组件。。。