Skip to content

Commit 6dd5f38

Browse files
toto8514toto8514
authored andcommitted
README.md - update middleware section
1 parent 8f965e7 commit 6dd5f38

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,18 @@ export default connect(mapStateToProp)(ReadFromStore);
214214
# Apply Middleware
215215

216216
As for Redux, middleware is a way to extend Redhooks with custom functionality.
217-
Middleware are functions which receive Store's `dispatch` and `getState` as named arguments, and return a function.
217+
Middleware are functions which receive Store's `dispatch` and `getState` as named arguments, and return a function. Redhooks supports the use of the redux's middlwares like `redux-thunk`, `redux-saga` or you migth write a custom middlware conforming to middleware API.
218+
219+
## Custom middlware - Logger Example
220+
221+
```js
222+
const logger = store => next => action => {
223+
console.log('dispatching', action)
224+
let result = next(action)
225+
console.log('next state', store.getState())
226+
return result
227+
}
228+
```
218229

219230
## Use `redux-thunk` and `redux-saga`
220231

@@ -253,15 +264,17 @@ App routing can be handled using [React Router](https://github.com/ReactTraining
253264
```js
254265
import React from 'react'
255266
import Provider from 'redhooks'
256-
import { BrowserRouter as Router, Route } from 'react-router-dom'
267+
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
257268
import Home from './Home'
258269
import About from './About'
259270

260271
const App = ({ store }) => (
261272
<Provider store={store}>
262273
<Router>
263-
<Route exact path="/" component={Home} />
264-
<Route exact path="/about" component={About} />
274+
<Switch>
275+
<Route exact path="/" component={Home} />
276+
<Route exact path="/about" component={About} />
277+
</Switch>
265278
</Router>
266279
</Provider>
267280
)
@@ -519,6 +532,7 @@ Following few open source projects implemented with `redux` have been migrated t
519532
* Shopping Cart: [Sandbox](https://codesandbox.io/s/5yn1258y4l)
520533
* TodoMVC: [Sandbox](https://codesandbox.io/s/7jyq991p90)
521534
* Tree-View: [Sandbox](https://codesandbox.io/s/rmw98onnlp)
535+
* Saga-Middlware: [Sandbox](https://codesandbox.io/s/48pomo7rx7)
522536

523537
# License
524538

src/store.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ export const createStore = (reducer, opts = {}) => {
7070

7171
/**
7272
You can call `useStore()` only within a function Component. It returns an object { state, dispatch }.
73-
To use the store from a class Component you have to use the `connect` HOC => export default connect(YourClassComponent)
73+
To use the store from a class Component you have to use the `connect` HOC.
74+
export default connect([mapStateToProps], [mapDispatchToProps])(YourClassComponent)
7475
*/
7576

7677
export const useStore = () => useContext(Context);

0 commit comments

Comments
 (0)