Dva: annoying warning: Warning: dispatch: ns/ACTION_TYPE should not be prefixed with namespace ns

Created on 18 Jun 2017  ·  11Comments  ·  Source: dvajs/dva

Is it possible to turn off the "helpful" warnings received when using the "dispatch" function in registrations or "put" effect?
Or at least make them optional.

I keep my action creators and actions types in dedicated files, which I believe is a general best practice for redux (see for example here)

This means that I always use my action creators in dva effects, registrations and components.

Example:
actionTypes.js:

export const TOKEN_RESTORED = 'auth/TOKEN_RESTORED';

actions.js:

export default {
    tokenRestored: payload => ({type: TOKEN_RESTORED, payload: payload.token})
};

model.js:

app.model({
    namespace: ...,
    state: { ... },
    effects: { ... },

    subscriptions: {
        setup({dispatch}) {
            let token = localStorage.token;
            if (token) {
                dispatch(actions.tokenRestored({token}));
            }
        }
    }
});

I believe that users should be able to write code such as this, without receiving dozens of warnings of the type:

Warning: dispatch: auth/tokenRestored should not be prefixed with namespace auth

Code style is a manner of personal preference, and dva currently punishes users that don't directly use strings for action types.

Most helpful comment

I'm a little late to the party here, but this is a hack I used to work around the error. Definitely not a good solution but it at least stifles the error output.

```// my-model.js
console.error = message => {
if (message.indexOf("prefixNamespace") === -1) {
console.log(message);
}
};

All 11 comments

I have just experienced same issue.
Managing actions by redux-actions is convenient.
However, this cannot used in the models because of the warning.

I've created a pull request in case that helps this issue getting attention.

1013

Recommend using https://www.npmjs.com/package/babel-plugin-dev-expression to strip invariant and warning for production.

What about development environment?
What if I cant use Babel plugins because I'm using create react app?

Why should I get warnings for using a common technique?

On Jul 19, 2017 06:25, "chencheng (云谦)" notifications@github.com wrote:

Recommend using https://www.npmjs.com/package/babel-plugin-dev-expression
to strip invariant and warning for production.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/dvajs/dva/issues/988#issuecomment-316261873, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ABJVKPRJMf0P0qVcTgOm-UH4q9RCNpg4ks5sPXcTgaJpZM4N9hlv
.

@orzarchi I believe @sorrycc is extremely busy and focused on opened issues only to be able to maintain this project. So you have to create a new issue in order to draw his attention.

BTW, did you manage to workaround these warnings?

I had a different problem. My model puts an action which is used only by other models:

// realtime.js
yield put({ type: 'message', payload: { message } });

and other models listen for messages from realtime:

// someModel.js
const message = yield take('realtime/message');

Since realtime model does nothing with message itself, it has no defined message effect. Because of that yield put({ type: 'message', payload: { message } }) was not prefixed by dvajs internal algorithms and realtime/message never fired.
And if I prefix manually:

// realtime.js
yield put({ type: 'realtime/message', payload: { message } });

it throws warnings about namespace ('... should not be prefixed with namespace ...')

To workaround the problem I’ve added dummy effect into model:

// realtime.js
'effects': {
  // dvajs requires effect to be defined to auto-prefix it with namespace
  // if add prefix manually then dvajs throws annoying warnings
  // https://github.com/dvajs/dva/issues/988
  message: () => { },
}

Now I have no warnings and other models receive realtime/message action.

I don't see why opening the issue again will change his opinion.
You solution seems okay, however the extra effort isn't worth it when you have hundreds of actions.
Also it's common practice to use constants for action types (or action creator functions), and as such I always dispatch with the prefix.
I haven't found a workaround yet, so I'm considering forking dva for my current project, and then never using it again.

I'm a little late to the party here, but this is a hack I used to work around the error. Definitely not a good solution but it at least stifles the error output.

```// my-model.js
console.error = message => {
if (message.indexOf("prefixNamespace") === -1) {
console.log(message);
}
};

I'm a little late to the party here, but this is a hack I used to work around the error. Definitely not a good solution but it at least stifles the error output.

console.error = message => {
  if (message.indexOf("prefixNamespace") === -1) {
    console.log(message);
  }
};

Can you clarify your code? where did you put the code?

You could put it in src/app.tsx. And if you want to keep console.error instead of converting all console.error to console.log, you could try this code

const errorLog = console.error;
  console.error = (...rest) => {
    if (rest[0] && rest[0].indexOf('[sagaEffects.put]') === -1) {
      errorLog.call(console, ...rest);
    }
  };

You could put it in src/app.tsx. And if you want to keep console.error instead of converting all console.error to console.log, you could try this code

const errorLog = console.error;
  console.error = (...rest) => {
    if (rest[0] && rest[0].indexOf('[sagaEffects.put]') === -1) {
      errorLog.call(console, ...rest);
    }
  };

Thank you, and I found that I can put anywhere in the app, :))

Was this page helpful?
0 / 5 - 0 ratings