- Node
- React Dev Tool
- Redux Dev Tool
git clone https://github.com/wesbos/Learn-Redux-Starter-Files.git
- Hello Reduxstagram (#1)
- Create a component and render it (#2)
- Define routes (#4)
- Create the store (#5)
- Create action creators (#6)
- Create reducers (#7)
- Wire and use store, action creators and reducers with react (#8)
- Inject data and dispatch to UI (#10)
- Display data in UI elements (#11)
- Handle user's action with a reducer to update state (#12)
- Create the Single view (#13)
- Create a comment lists and a comment form (#14)
- Create UI handler to dispatch the comment action (#15)
- Comment reducer update state change requests (#16)
- Hot reload reducers (#18)
- Redux dev tool (#19)
- root reducer delegates to all reducers to change state and each reducer needs to decide how to (or not to) change it. Root reducer then combines the changes.
- an action creator returns an action. An action is a json object which describes user intention.
- connect takes 2 params
- data in state to be injected
- binding between action creators and dispatch
- connects return a connect object, which can then bind to a UI element
- use slice to create the next state
[
...state.slice(0,i),
{...state[i], updatedData: "new data"},
...state.slice(i+1)
]
- dispatch an action with inline HTML
onClick={this.props.increment.bind(null, i)}
- dispatch an action in js
this.props.increment(i)
- hot reload reducers
if (module.hot) {
module.hot.accept('./reducers/', () => {
const nextRootReducer = require('./reducers/index').default;
store.replaceReducer(nextRootReducer);
});
}
- use redux dev tool
const enhancers = compose(
window.devToolsExtension ? window.devToolsExtension() : f => f
);
const store = createStore(rootReducer, defaultState, enhancers);
react-router
manages routes and declare root level.Link
creates anchor tag
react-redux
provides binding between redux and reactProvider
declares what store to useconnect
ties UI components and data
react-addons-css-transition-group
CSSTransitionGroup
- webpack packages the bundle - webpack.config.js defines the entry point (ie reduxstagram.js) and the output bundle name (ie bundle.js)
- browser loads index.html, which includes the generated bundle.js
- reduxstagram.js renders the application at an element of index.html
- load
store
viaProvider
tag (react-redux
)createStore()
(redux
) createsstore
by passing the root reducercombineReducers
(redux
) creates the root reducer by packaging all reducers
Router
tag (react-router
) specifies route configuration and the container component (ie App)- App use
connect()
(react-redux
) to expose the state slice instore
andaction creators
to the root UI element
- App use
- load
Redux code
import { createStore, combineReducers } from 'redux';
// create reducer
function posts (state = [], action) {
return state
}
// combine reducers into a root reducer
const rootReducer = combineReducers({posts})
// create redux store
const store = createStore(rootReducer)
// create action creator
function increment(index) {
return {
type: 'INCREMENT_LIKES',
index
}
}
React Redux
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
// expose state data to UI via props
function mapStateToProps(state) {
return {
posts: state.posts,
comments: state.comments
}
}
// expose action creators to UI via props
function mapDispatchToProps(dispatch) {
return bindActionCreators(actionCreators, dispatch);
}
// export App component to react
export const App = connect(mapStateToProps, mapDispatchToProps)(Main);
Redux
const router = (
<Provider store={store}>
<Router history={history}>
<Route path="/" component={App}>
<IndexRoute component={PhotoGrid}></IndexRoute>
<Route path="/view/:postId" component={Single}></Route>
</Route>
</Router>
</Provider>
)
render(router, document.getElementById('root'));