redux-store

The Goal

  • Create a store using react-redux to manage everything

  • Put it in index.js

  • Wrap it in a Provider element from react-redux

// ...as before...
import robotsSearch from './reducers'
import { createStore } from 'redux'
import { Provider } from 'react-redux'

const store = createStore(robotsSearch)

ReactDOM.render(
  <Provider store={store}>
    <App/>
  </Provider>,
  document.getElementById('root')
);
  • createStore creates a Redux store

    • We still need to provide specific actions and dispatchers

  • Provider is a way to create the equivalent of a global variable

    • Adds named data to the props of all of the elements within its scope

  • We won't actually refer to store ourselves

    • It will be connected when we wire everything together

Connecting the Pieces

  • Modify App.js (our container)

  • connect takes two functions

  • Wraps the App class with something that knows how to talk to a default store

    • This code relies on the store variable created by the Provider

Mapping State to Properties

  • Given a state, generate the change to the properties

Mapping Dispatch Function to Properties

Constructor

  • Initialize state as usual

Initial Data Fetch

  • As before

Rendering

  • Looks pretty much the same as well

    • Which is the point

  • Get onSearchChange from the properties

    • This is the function named in the object returned by mapDispatchToProps

    • react-redux wires all of this up for us

Last updated

Was this helpful?