Writing Redux-like simple middleware for React Hooks
React 16.8 came with the addition of React Hooks. I was curious about how far it goes to allow you to replace Redux and create a middleware like Redux Thunk, Observable or Saga.
There are a number of articles about the new method for managing local state with useState
and the global context by using useContext
. There are some articles about how to connect that with your API calls to your backend but they all seem to propose a solution based on useEffect
which in my mind is flawed.
The solution that I have seen is to create a local state which contains URL leading to your server:
When your component then wants to trigger API call it uses setUrl
function to and useEffect
with axios library to make that happen like this: