2017-10-17 93 views
-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 .

+0

你能否包含完整的错误? –

+0

更新了我的线程,但是在这里它是'invariant.js?4599:44未捕获的错误:元素类型无效:期望一个字符串(对于内置组件)或类/函数(对于复合组件),但得到:object。检查'StatelessComponent'的渲染方法.' – sthig

+0

我在这里看不到任何无状态的组件... –

回答

0

这是试图现在访问我的mobile.js,它所有的作品一个旧版本的一些恶意代码。

相关问题