-1
我在嘗試將無狀態組件導入到我的App.js中以運行時收到錯誤type is invalid
。當我在App.js中運行相同的代碼時,它工作得很好,但是當我導入它時,它會中斷。這裏有什麼解決方案,發生了什麼?react.js無狀態組件導入時出現「無效」錯誤
預先感謝您
我mobile.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,給我的type is invalid
錯誤。
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>
);
}
}
不知道該怎麼辦,從這裏修復它。完整的錯誤是
invariant.js?4599:44 Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. Check the render method of
StatelessComponent .
你能否包含完整的錯誤? –
更新了我的線程,但是在這裏它是'invariant.js?4599:44未捕獲的錯誤:元素類型無效:期望一個字符串(對於內置組件)或類/函數(對於複合組件),但得到:object。檢查'StatelessComponent'的渲染方法.' – sthig
我在這裏看不到任何無狀態的組件... –