简体   繁体   English

警告:道具类型失败:提供给“提供者”的对象类型为“对象”的道具儿童无效,需要一个ReactElement

[英]Warning: Failed prop type: Invalid prop `children` of type `object` supplied to `Provider`, expected a single ReactElement

I have a problem: 我有个问题:

Warning: Failed prop type: Invalid prop children of type object supplied to Provider , expected a single ReactElement.

I have searched for an answer for sometime now but can't resolve this. 我已经搜索了一段时间,但无法解决。 My version of react is 15.3.0. 我的React版本是15.3.0。

thanks~ 谢谢〜

    import React from 'react';
    import ReactDom from 'react-dom';
    import { createStore, applyMiddleware, combineReducers } from 'redux';
    import { Provider } from 'react-redux';
    import { Router, Route, IndexRoute, hashHistory } from 'react-router';
    import { syncHistoryWithStore, routerReducer } from 'react-router-redux';
    import thunkMiddleware from 'redux-thunk';

    import reducers from './redux/reducers';
    import Entrance from './components/Entrance.jsx';
    import EntranceNone from './components/EntranceNone.jsx';
    import CreditPoints from './components/CreditPoints.jsx';
    import CreditPrivilege from './components/CreditPrivilege.jsx';
    import CreditPromote from './components/CreditPromote.jsx';

    let reduxDevTool = null;
    let combReducer = { app: reducers, routing: routerReducer };
    let store = createStore(combineReducers(combReducer), applyMiddleware(thunkMiddleware));

    const history = syncHistoryWithStore(hashHistory, store);

    ReactDom.render(
        <Provider store={store}>
            <div>
                <Router history={history}>
                    <Route path="/" component={ Entrance }></Route>
                    <Route path="/EntranceNone" component={ EntranceNone }></Route>
                    <Route path="/creditPoints" component={ CreditPoints }></Route>
                    <Route path="/privilege" component={ CreditPrivilege }></Route>
                    <Route path="/promote" component={ CreditPromote }></Route>
                </Router>
            </div>
        </Provider>,
        document.getElementById('app')
    );


require('../less/entrance.css');
import { initRootFontSize,originDomin } from '../utils';
import React,{Component} from 'react';
import classNames from 'classnames';
import 'babel-polyfill';
import cookie from 'react-cookie';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as Actions from '../redux/action';
import { hashHistory ,Link } from 'react-router';

class Entrance extends Component { 
    constructor (props) {
        super(props);
        initRootFontSize();
        this.state = {
            agree:true,
            submitFlag:false
        };
    }

    handleAgreeMent(){
        this.setState({agree:!this.state.agree});
    }

    componentDidMount(){
        if(creditData.isOpenCreditService === "0"){
            this.context.router.push('/');
        }else{
            this.context.router.push('/creditPoints');
        }
    }

    componentDidUpdate(prevProps,prevState){
        if(this.props.resCode && this.state.submitFlag){
            if(this.props.resCode == "000"){
                this.context.router.push('/creditPoints');
            }else{
                alert(this.props.resMsg);
            }
        }
    }

    handleClick(){
        if(this.state.agree){ 
            this.props.actions.fetchIsOpen();
            this.setState({submitFlag:true}); 
        }
    }

    render () {   
        return(
            <div className="wrap">
                <div className="credit-img"></div>
                <div className="credit-cont">
                    <p className="credit-cont-up">xxx</p>
                    <p className="credit-cont-down">xxx</p>
                </div>
                <div className="credit-agree"><span className={classNames({icon: true, iconSelected: this.state.agree})} onClick={this.handleAgreeMent.bind(this)}></span><span className="credit-agree-cont">xx<a href={originDomin()+'static.pay.xiaomi.com/mi-credit-points/src/html/agreeMent.html'} className="credit-agree-detail">xxx</a></span></div>
                <div className={classNames({button: true,bottonFalse:this.state.agree,submitFlag:this.state.submitFlag})}  onClick={this.handleClick.bind(this)}>{this.state.submitFlag?'x':'xx'}</div>
            </div>
        );
    }

}

function mapStateToProps(state) {
    return {
        resCode: state.app.resCode,
        resMsg: state.app.resMsg,
        dataList: state.app.dataList
    };
}

function mapDispatchToProps(dispatch) {
    return {
        actions: bindActionCreators(Actions, dispatch),
    };
}

Entrance.contextTypes = {
    router: React.PropTypes.object
};

export default connect(mapStateToProps, mapDispatchToProps)(Entrance);

I'm going to make a few assertions, because there are so few details, but this error happens in a very specific situation which is explained later in detail. 我将作一些断言,因为细节太少了,但是此错误发生在非常特殊的情况下,稍后将对此进行详细说明。 Specifically it happens if either React or ReactDOM (but not both) is imported before babel-polyfill. 具体来说,如果在babel-polyfill之前导入了React或ReactDOM(但不是全部),则会发生这种情况。 If both are imported before, that's fine. 如果两者都以前导入,那很好。 If both are imported after, that's also fine. 如果两者都是在以后导入的,那也很好。 Now, the assertion of my answer is that you're using react-hot-loader/patch as your first entry point, instead of babel-polyfill . 现在,我的答案是断言是您使用react-hot-loader/patch作为您的第一个入口点,而不是babel-polyfill

Babel-polyfill documentation clearly states on their front page no less : Babel-polyfill文档清楚地在其首页上注明:

To include the polyfill you need to require it at the top of the entry point to your application. 要包括polyfill,您需要在应用程序入口点的顶部要求它。

Make sure it is called before all other code/require statements! 确保在所有其他代码/要求语句之前调用它!

While you can work around this requirement, you have to be careful if doing so. 虽然可以解决此要求,但必须小心。 So the simplest solution to your issue, presuming webpack, is to have your entry point configuration as follows 因此,假设您是webpack,最简单的解决方案是对入口点进行如下配置

entry: {
    'babel-polyfill',
    'react-hot-loader/patch',
    ...
}

This is not the only solution, read the full explanation below to see how to avoid this issue if adding an entry point is not an option (for example due to desire to reduce entry point chunk size) 这不是唯一的解决方案,请阅读下面的完整说明,以了解如果无法添加入口点,如何避免此问题(例如,由于希望减小入口点块大小)

Technical explanation 技术说明

react-hot-loader/patch in patch.dev.js imports React . patch.dev.js中的 react-hot-loader/patch导入React Because babel-polyfill is not yet loaded and Symbol is not yet available on certain browsers, React sets its REACT_ELEMENT_TYPE constant to numeric form instead of Symbol . 因为尚未加载babel-polyfill polyfill ,并且在某些浏览器上Symbol尚不可用,所以React将其REACT_ELEMENT_TYPE常量设置为数字形式,而不是Symbol ReactDOM will do this same constant definition for itself independently of React. ReactDOM将独立于React为其自身执行相同的常量定义。 However if babel-polyfill is now imported before ReactDOM, Symbol is now available when ReactDOM is imported and therefore the constant for ReactDOM gets defined in the Symbol form rather than numeric form. 但是,如果现在babel-polyfill是在ReactDOM之前导入的,那么符号在导入ReactDOM时现在可用,因此ReactDOM的常量将以Symbol形式而不是数字形式定义。 Therefore your React and ReactDOM have a different REACT_ELEMENT_TYPE and ReactDOM is unable to distinguish React Components from other objects. 因此,您的React和ReactDOM具有不同的REACT_ELEMENT_TYPE,并且ReactDOM无法将React组件与其他对象区分开。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 失败的道具类型:提供给`Body`的无效道具&#39;app`,期望一个ReactElement - Failed prop type: Invalid prop `app` supplied to `Body`, expected a single ReactElement 反应错误'Failed propType:提供给`Provider`的无效道具`children`,期望一个ReactElement' - React error 'Failed propType: Invalid prop `children` supplied to `Provider`, expected a single ReactElement' 警告:失败的道具类型:提供给`Dropzone`的类型`string`的prop`child`无效,期望的`function` - Warning: Failed prop type: Invalid prop `children` of type `string` supplied to `Dropzone`, expected `function` 获取错误:警告:道具类型失败:提供给“Form”的道具“children”无效,应为 ReactNode - GETTING ERROR : Warning: Failed prop type: Invalid prop `children` supplied to `Form`, expected a ReactNode 警告:失败的道具类型:提供给“路由”的“对象”类型的无效道具“组件”,预期的“函数”使用 react-router-dom 错误 - Warning: Failed prop type: Invalid prop `component` of type `object` supplied to `Route`, expected `function` Error using react-router-dom 警告:道具类型失败:提供给“ForwardRef(FormControl)”的“string”类型的道具“error”无效,预期为“boolean” - Warning: Failed prop type: Invalid prop `error` of type `string` supplied to `ForwardRef(FormControl)`, expected `boolean` 道具类型失败:提供给“CSSTransition”的道具“children”无效 - Failed prop type: Invalid prop `children` supplied to `CSSTransition` 警告:道具类型失败:提供给“路线”的道具属性无效。 在途中 - Warning: Failed prop type: Invalid prop `component` supplied to `Route`. in Route 警告:道具类型失败:提供给 `ForwardRef(Slider)` 的道具 `value` 无效 - Warning: Failed prop type: Invalid prop `value` supplied to `ForwardRef(Slider)` 警告:道具类型失败:提供给“图片”的道具“源”无效 - Warning: Failed prop type: Invalid prop `source` supplied to `Image`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM