reduxjs / redux

A JS library for predictable global state management
https://redux.js.org
MIT License
60.88k stars 15.27k forks source link

Dispatch "Use custom middleware for async actions" is confusing and should be updated #4028

Closed markerikson closed 3 years ago

markerikson commented 3 years ago

The core dispatch function currently starts with:

  function dispatch(action) {
    if (!isPlainObject(action)) {
      throw new Error(
        'Actions must be plain objects. ' +
          'Use custom middleware for async actions.'
      )
    }

Frankly, I've always disliked the "async actions" term, which is part of why I avoided it completely in the new tutorials. The phrase seems confusing anyway.

On top of that, this doesn't actually tell you what was dispatched, which might be helpful for knowing what the actual problem is - is it a Promise, a function, undefined, or something else?

We should reword this to drop the "async actions" phrase, and also have it log the received value.

I'm wondering if there's a good way to log the value itself. I know that over in React-Redux, we have a stringifyComponent function that tries to provide more details and falls back if it can't be stringified:

const stringifyComponent = (Comp) => {
  try {
    return JSON.stringify(Comp)
  } catch (err) {
    return String(Comp)
  }
}

maybe something like that would be relevant here?

This should be doable as a patch release for 4.x, so presumably 4.0.6.

timdorr commented 3 years ago

I think the only issue is if the object is really large, then the JSON.stringify output would be as well. I don't know if there's a good workaround so we don't end up spamming the console log with a bunch of junk.

markerikson commented 3 years ago

slice the string to a max of 100 characters or something? :)

markerikson commented 3 years ago

I'm playing with inlining a cut-down version of https://github.com/jonschlinkert/kind-of in dev mode. That at least prints out 'Promise', 'Date', etc, instead of just 'object', and then it falls back to typeof in prod.

markerikson commented 3 years ago

Live now in https://github.com/reduxjs/redux/releases/tag/v4.1.0-alpha.0

markerikson commented 3 years ago

Could still consider logging the actual values rather than the name of the type, if we think that would be beneficial.