HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptreactCritical

How can one tell the version of React running at runtime in the browser?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
runningonebrowserthereactversioncanhowtellruntime

Problem

Is there a way to know the runtime version of React in the browser?

Solution

React.version is what you are looking for.

It is undocumented though (as far as I know) so it may not be a stable feature (i.e. though unlikely, it may disappear or change in future releases).

Example with React imported as a script



const REACT_VERSION = React.version;

let root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
React version: {REACT_VERSION}
);







Example with React imported as a module

import { version } from 'react';

console.log(version);


Obviously, if you import React as a module, it won't be in the global scope. The above code is intended to be bundled with the rest of your app, e.g. using webpack. It will virtually never work if used in a browser's console (it is using bare imports).

This second approach is the recommended one. Most websites will use it. create-react-app does this (it's using webpack behind the scene). In this case, React is encapsulated and is generally not accessible at all outside the bundle (e.g. in a browser's console).

Code Snippets

import { version } from 'react';

console.log(version);

Context

Stack Overflow Q#36994564, score: 201

Revisions (0)

No revisions yet.