I've been following the ReduxJS documentation here: http://redux.js.org/docs/basics/UsageWithReact.html
At the end of the document it mentions usage of the provider object, I have wrapped my App component in the provider like so:
import React from 'react'
import ReactDOM from 'react-dom'
import { createStore } from 'redux'
import { Provider } from 'react-redux'
import RootReducer from './app/reducers'
import App from './app/app'
const store = createStore(RootReducer)
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)
However, that's where the documentation ends. How do I pickup the store provided by provider within the components?
Thanks for the answer, but it is missing one crucial bit, which is actually in the documentation.
So I had to add the following for it to work for me:
The best way to access your store through a component is using the
connect()
function, as described in the documentation. This allows you to map state and action creators to your component, and have them passed in automatically as your store updates. Additionally, by default it will pass indispatch
asthis.props.dispatch
. Here's an example from the docs:Note that
connect
actually creates a new component that wraps around your existing one! This pattern is called Higher-Order Components, and is generally the preferred way of extending a component's functionality in React (over stuff like inheritance or mixins).Due to it having quite a few performance optimizations and generally being less likely to cause bugs, the Redux devs almost always recommend using
connect
over accessing the store directly - however, if you have a very good reason to need lower level access, theProvider
component makes it so all its children can access the store throughthis.context
:To use Redux Provider with react.You need to use a module i.e
React-redux provide
connect()
method which helps in binding the store from the provider to React component.Using method like
mapDispatchToProps
andmapStateToProps
we can create a condition in which the component is rendered .
Ex:
Now we get dispatch method provided by Provider and can use in our component to trigger any action
ex: