lukashala / react-simply

Tools for simple React development
MIT License
251 stars 22 forks source link

Middleware Support #4

Open tiagobnobrega opened 5 years ago

tiagobnobrega commented 5 years ago

Hi... First of all, thx for sharing this solution. I've found a way to support middleware/enhancers for This solution, there's still some testing and validation. Do You think It could be a nice addition?

lukashala commented 5 years ago

Hi, feel free to create pull-request.

ehhMoises commented 5 years ago

Hi @lukashala Could you give an example how implement a Middleware? It could be possible apply a dispatcher in such Middleware?

By the way. Your solution it's great one!

lukashala commented 5 years ago

Hi @ehhMoises, see my article here, I mention it at bottom part https://medium.com/simply/state-management-with-react-hooks-and-context-api-at-10-lines-of-code-baf6be8302c

ehhMoises commented 5 years ago

Hi Again @lukashala

I would like to know, how do i send the dispatch's context to middleware? It is possible in such context?

lukashala commented 5 years ago

Hi @ehhMoises, everything is possible :) One way could be to create some higher order function and wrap reducer with it.

Something like this:

const reducer = (state, action) => {...}

const withMiddleware = (reducer) => (state, action) => {
  doAnythingYouWantHere();
  return reducer(state, action);
}

const reducerWithMiddleware = withMiddleware(reducer);
chestermjr commented 4 years ago

https://transang.me/get-state-callback-with-usereducer/