facebookexperimental / Recoil

Recoil is an experimental state management library for React apps. It provides several capabilities that are difficult to achieve with React alone, while being compatible with the newest features of React.
https://recoiljs.org/
MIT License
19.56k stars 1.18k forks source link

[SSR][NextJS] Duplicate atom key during development and during production build in nextjs #733

Closed janus-reith closed 1 year ago

janus-reith commented 3 years ago

Both during local development and also when building a production build, I’m getting a duplicate atom key for each key and each page using it after the first one.

I put together a quick sandbox to demonstrate the issue: https://codesandbox.io/s/flamboyant-sea-tqlky

The errors show up in the build log, a quick way to test is using the integrated vercel deployment button. Looking at the default nextjs example, I can‘t spot any special settings there that would prevent whatever duplication is going on from happening: https://github.com/vercel/next.js/tree/canary/examples/with-recoil That example however only makes use of recoil state on one page.

juanpprieto commented 1 year ago

we got there in the end <3

christian-gama commented 1 year ago

Just faced this issue... Came here and saw this patch 0.7.6 released yesterday. Am I lucky? :laughing:

aam051102 commented 1 year ago

@wfortin is correct. Recoil 0.7.6 with RECOIL_DUPLICATE_ATOM_KEY_CHECKING_ENABLED=false in .env solved the issue in our NextJS project. Thank you very much for that information.

coderhzy commented 1 year ago

const intercept = require('intercept-stdout');

// safely ignore recoil stdout warning messages function interceptStdout(text) { if (text.includes('Duplicate atom key')) { return ''; } return text; }

intercept(interceptStdout);

Mecil9 commented 1 year ago

I updated to version 0.7.6 and added the RECOIL_DUPLICATE_ATOM_KEY_CHECKING_ENABLED=false environment variable and it solved it for me. I think this ticket can be closed now.

I've been waiting a year for this answer!!!!!!

hamtarodev commented 1 year ago

how can i do this with Vite? on Vite it requires that the env variables should start with VITE_

satokoki645 commented 1 year ago

how can i do this with Vite? on Vite it requires that the env variables should start with VITE_

I was thinking this same thing.

ThomasCarstens commented 1 year ago

@hamtarodev @SATOKOKI645 The recoil changelog will help you, as of 6/10/22: https://yarnpkg.com/package/recoil

tesseractjh commented 1 year ago

@ThomasCarstens Thanks! @hamtarodev @SATOKOKI645 RecoilEnv can solve it with Vite.

// configs/recoil.ts
import { RecoilEnv } from 'recoil';

RecoilEnv.RECOIL_DUPLICATE_ATOM_KEY_CHECKING_ENABLED = false;
// App.tsx
import './configs/recoil';

function App() {
  return (
    <RecoilRoot>
        { ... }
    </RecoilRoot>
  );
}
qqww08 commented 1 year ago

Thanks to this, I was able to remove the console log from nextJS. This is my code

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  env: {
// @see https://github.com/facebookexperimental/Recoil/issues/2135#issuecomment-1362197710
    RECOIL_DUPLICATE_ATOM_KEY_CHECKING_ENABLED: "false",
  }
};

module.exports = nextConfig;
LOGANLEEE commented 1 year ago

@ThomasCarstens Thanks! @hamtarodev @SATOKOKI645 RecoilEnv can solve it with Vite.

// configs/recoil.ts
import { RecoilEnv } from 'recoil';

RecoilEnv.RECOIL_DUPLICATE_ATOM_KEY_CHECKING_ENABLED = false;
// App.tsx
import './configs/recoil';

function App() {
  return (
    <RecoilRoot>
        { ... }
    </RecoilRoot>
  );
}

solved problem.

in my case

lovely thanks.

MejanH commented 1 year ago

Just because of this issue I had to move to Jotai. disabling the Duplicate key checker is not a fix. now, recoil won't check for duplicate keys.

SGDS666 commented 1 year ago

I tried all the above methods but they didn't solve my problem. Every time I refresh the page, I report an error, so I rudely used the following method to solve the problem

console.error = (messages, ...rest) => {
  if (messages?.message?.includes("Duplicate atom key")) {
    return
  }
  console.warn(messages, ...rest)
}
mickdewald commented 1 year ago

Just because of this issue I had to move to Jotai. disabling the Duplicate key checker is not a fix. now, recoil won't check for duplicate keys.

It's said, I know. I will check out Zustand, as I already have for my mobile app. It works like a charm