React-redux使用中有关Provider问题

react

先上错误:

  Warning: Failed prop type: Invalid prop `children` of type `array` supplied to `Provider`, expected a single ReactElement. in Provider (at index.js:8)

  React.Children.only expected to receive a single React element child.

代码:index.js

import React from 'react';

import ReactDOM from 'react-dom';

import {Provider} from 'react-redux'

import store from './redux/Store'

import App from './self-component/Router'

ReactDOM.render(<Provider store={store} > <App /> </Provider> ,document.getElementById('root'))

原因:

  原因是吧Provider放错地方了。应该放在路由表的根路由的位置。这样每次数据修改后,就会从根路由开始重新渲染其他组件

改正:

  就是在要 return 的最外层包一层 <Provider>...</Provider>   main.js

import React,{Component} from 'react'

import {Link, IndexLink, hashHistory} from 'react-router'

import {Provider} from 'react-redux'

import '../styles/common.css'

import '../styles/main/main.css'

class Main extends Component{

render(){

return(

<Provider>

<div className="main">

<div className='component'>

{this.props.children}

</div>

<div className='foot'>

<div className='foot-block'>

<IndexLink to='/' activeClassName='active'>

<dl className="homePage">

<dt className="iconfont">&#xe50a;</dt>

<dd>首页</dd>

</dl>

</IndexLink>

</div>

</div>

</div>

</Provider>

)

}

}

export default Main

以上是 React-redux使用中有关Provider问题 的全部内容, 来源链接: utcz.com/z/381358.html

回到顶部