1

If I want to check whether React's strict mode is enforced in JavaScript, there are some solutions here.

However, I can't get any of them to work when I translate them to TypeScript.

I've looked online for a TypeScript boolean method for checking if I'm in Strict mode, but can't find one. Can anyone help, please?

Thanks in advance.

Jeff W
  • 61
  • 7

1 Answers1

1

OLD ANSWER(INCORRECT):

Maybe you can just simply use a file in the same directory as the configuration file. And read the tsconfig using the built in fs module and parsing it using json parse.

Hope this helps.

NEW ANSWER:

I know it's quite hacky but, off the top of my head, the only solution that I can think of is you should write your component in a string and use Bable.transform() to transform it on the fly while the app is running. This way, you can now use the String.prototype.includes() function to search for the React.StrictMode JSX element.

Hope this will help you in your project, once again, I know it's quite hacky and not the best solution.

Have a great day!

Alvin CHENG
  • 374
  • 2
  • 16
  • Thanks Alvin. The tsconfig file would tell me whether TypeScript strict mode is on. However, what I'm interested in is React strict mode. I've edited my question title to make that clearer. I've tried reading index.tsx file and searching for the string 'React.StrictMode'. However, doing so produced the HTML of index.html instead of just giving me the TypeScript code! Given that strict mode runs useEffect twice, I'm surprised my question hasn't been answered previously. You don't want to be changing your code when you remove strict mode before going to production. – Jeff W Jul 02 '23 at 11:52
  • Ok Sorry for the misunderstanding and confusion caused – Alvin CHENG Jul 03 '23 at 05:52
  • I have edited it, so please take a look! – Alvin CHENG Jul 03 '23 at 06:09
  • Thanks Alvin - and no problem at all. :) – Jeff W Jul 03 '23 at 08:53