I am trying to connect the state in the reducers to the components in my react components.
Here is the code of the reducer is in reducer_books.js
:
export default function(){
return [
{title:'A'},
{title:'E'}
];
}
Then, i added it to the combine reducer in index.js
:
import {combineReducers} from 'redux';
import BookReducer from './reducer_books';
const rootReducer= combineReducers({
books:BookReducer
});
export default rootReducer;
At the end i tried to add it in the parent component:
import State from './reducer/index';
import { Provider } from 'react-redux';
ReactDOM.render(
<Provider store={State}>
<App />
</Provider>, document.getElementById('root'));
And in the target component i have:
import React,{Component} from 'react';
import {connect} from 'react-redux';
class BookList extends Component{
renderList(){
return this.props.books.map((book)=>{
<li key={book.title}>{book.title}</li>
});
}
render(){
return(
<div>
<ul>
{this.renderList()}
</ul>
</div>);
}
}
function mapStateToProps(state){
return{
books:state.books
};
}
export default connect(mapStateToProps)(BookList);
Unfortunately, it complains with:
Warning: Failed prop type: Invalid prop `store` of type `function` supplied to `Provider`, expected `object`.
in Provider (at index.js:11)
Warning: Failed childContext type: Invalid child context `store` of type `function` supplied to `Provider`, expected `object`.
in Provider (at index.js:11)
Warning: Failed context type: Invalid context `store` of type `function` supplied to `Connect(BookList)`, expected `object`.
in Connect(BookList) (at App.js:35)
in div (at App.js:33)
in div (at App.js:32)
in App (at index.js:12)
in Provider (at index.js:11)
Uncaught TypeError: _this.store.getState is not a function
Error: Attempted to update component `Connect(BookList)` that has already been unmounted (or failed to mount)
In your example I don't see where you configure Store
, I suppose that you missed this step
import reducers from './reducer/index';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
const Store = createStore(reducers);
ReactDOM.render(
<Provider store={ Store }>
<App />
</Provider>, document.getElementById('root')
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With