!rant - I'm really amazed what you can achieve with React / React Native and Redux with Sagas. I built a small React.js app with a map and some components which depend on each other - partly using stuff I wrote for a React Native app - on some evenings of my winter holidays. It's such a nice clean workflow and in the middle of it electron came to my mind. So I did npm run build and put the output inside the electron example project. A bit of configuration and this thing now runs on linux, mac and windows as well. Really awesome...

  • 3
    I don't understand why we need middleware for redux, where can I read their difference with and without?
  • 1
    @devTea You can handle async parts apart from the state. My login reducer has 4 actions and one saga. The actions are: sendLogin, loginSuccessful, loginFailed and logout. The reducer on sendLogin only alters the state (one boolean) to inform a login request is pending. The middleware is triggered on this action and does the API call. When the response is received, the reducer is called with either the loginSuccessful or failed action to then update the state with the user or error information. This way you can do also cross module functionality like: Update x after user has logged in.

    This would be the main part of the saga middleware:

    export default function* root() {
    yield all([
    takeLatest(SEND_LOGIN, sendLogin),

    // Refresh legends when requested or on login / logout
    takeLatest(LEGENDS_REFRESH, getLegends),
    takeLatest(LOGIN_SUCCESSFUL, getLegends),
    takeLatest(LOGOUT, getLegends),
  • 1
    @devTea Sorry, I don't have a link - you can most likely do these things also without the middleware by just dispatching actions on callback, or after the async operation. Thinking about it, it seems to me everything is possible without using middleware. I just really like having the added structure by pulling out async operations and having this one place where cross module dependencies are done, I think it really helps maintenance of a more complex application.
  • 1
    @devTea I don't even understand why do we need redux
  • 1
    @hack If you don't feel you should improve on how you handle application state, you're probably doing perfectly fine with your current approach.
  • 2
    @hack you need it to store something like global state, the only time I use redux is for login state tho
  • 1
    @devTea Are you mainly doing React.js, or also React Native? Most applications I work on use a map and geolocation, so I can make use of it as well for tracking user's gps position and also for passing information from click events on the map to different components. Redux seems to me to be a bit more relevant with smartphone apps, where you probably are more concerned with state than on a webapp.
  • 2
    @malphas I understand the need of redux, it does make your state looks cleaner, my problem is just the middleware. I’m still trying to figure it out, and I’m only using reacts.js
Your Job Suck?
Get a Better Job
Add Comment