diff --git a/step2-07/demo/README.md b/step2-07/demo/README.md index 6404041..a200c14 100644 --- a/step2-07/demo/README.md +++ b/step2-07/demo/README.md @@ -47,11 +47,7 @@ const NewComponent = connect( `connect()` takes in a few functions that map portions of the state tree and dispatcher functions into props. It is a **higher-order function**, meaning that the return value of `connect()` is a function that decorates `OldComponent` into a `NewComponent` with all the mapped props. -#### `mapStateToProps` - -A [`mapStateToProps`](https://react-redux.js.org/api/connect#mapstatetoprops-state-ownprops-object) function returns portions of the state tree that will be passed to the component as props. - -Under the hood, this function helps inform the connected view when it should re-render based on a shallow comparison with the previous state. +Next we'll learn about how to write `mapStateToProps` and `mapDispatchToProps`. For demonstration purposes, we'll assume the store and the component props look like this: ```ts interface Store { @@ -59,12 +55,18 @@ interface Store { // and probably some other properties } -interface FooProps { +interface ComponentProps { foo: string; - bar: string; + addTodo: (label: string) => void; } +``` -function mapStateToProps(state: Store): Partial { +#### `mapStateToProps` + +A [`mapStateToProps`](https://react-redux.js.org/api/connect#mapstatetoprops-state-ownprops-object) function uses the state tree as a parameter and selects portions of it that will be passed to the component as part of `props`. When values in the state tree change, the `mapStateToProps` function is called, and the new props are passed to the component (causing React to re-render it). + +```ts +function mapStateToProps(state: Store): Partial { return { foo: state.foo }; @@ -73,17 +75,10 @@ function mapStateToProps(state: Store): Partial { #### `mapDispatchToProps` -A [`mapDispatchToProps`](https://react-redux.js.org/api/connect#mapdispatchtoprops-object-dispatch-ownprops-object) function generates props which are used to dispatch Redux actions. - -This function generally returns props which the component will use as callbacks in response to user actions. +A [`mapDispatchToProps`](https://react-redux.js.org/api/connect#mapdispatchtoprops-object-dispatch-ownprops-object) function generates props which are used to dispatch Redux actions. This function generally returns props which the component will use as callbacks in response to user actions. ```ts -interface FooProps { - foo: string; - addTodo: (label: string) => void; -} - -function mapDispatchToProps(dispatch: any): Partial { +function mapDispatchToProps(dispatch: any): Partial { return { // the dispatched message COULD be generated by an // action creator instead (see later steps)