[Recoil ver 0.0.10] "Duplicate atom key" message shown in HMR env

When we use Recoil version 0.0.10 on HMR (Hot Module Replacement) environment, we encounter the following message on the console.

Duplicate atom key

Duplicate atom key "user_language". This is a FATAL ERROR in
      production. But it is safe to ignore this warning if it occurred because of
      hot module replacement. undefined

When appearing this message, you can ignore if you use HMR or derived system like Webpack, React and Next.js etc.

Why "Duplicate atom key"

このメッセージはRecoilのatomに設定するキーは一意でなければならないということを知らせています。

import { atom } from "recoil";

const testState = atom({key: 'user_language', default: 0});

Thus, the key you set when defining an atom must be unique. If it is not unique, there is a problem with the behavior and we warn you about it with a message like this However, in the HMR environment, every time a module is replaced, the key registration process runs.

However, in the HMR environment, every time a module is replaced, the key registration process runs. It seems to occur.

The following code shows the exact.

function registerNode<T>(node: Node<T>): RecoilValue<T> {
  if (nodes.has(node.key)) {
    const message = `Duplicate atom key "${node.key}". This is a FATAL ERROR in
      production. But it is safe to ignore this warning if it occurred because of
      hot module replacement.`;
    // TODO Need to figure out if there is a standard/open-source equivalent to see if hot module replacement is happening:
    // prettier-ignore
    // @fb-only: if (__DEV__) {
      // @fb-only: const isAcceptingUpdate = require('__debug').isAcceptingUpdate;
      // prettier-ignore
      // @fb-only: if (typeof isAcceptingUpdate !== 'function' || !isAcceptingUpdate()) {
        // @fb-only: expectationViolation(message, 'recoil');
      // @fb-only: }
    // prettier-ignore
    // @fb-only: } else {
      recoverableViolation(message, 'recoil');
    // @fb-only: }
  }
  nodes.set(node.key, node);

  const recoilValue: RecoilValue<T> =
    node.set == null
      ? new RecoilValueClasses.RecoilValueReadOnly(node.key)
      : new RecoilValueClasses.RecoilState(node.key);

  recoilValues.set(node.key, recoilValue);
  return recoilValue;
}

We have to ignore this message on this version (Recoil version 0.0.10). As the comment, the developer team will fix it in near future!

Updated at: Wed Jul 01 2020

© 2020-presentTerms|Privacy