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

前端 未结 12 1785
抹茶落季
抹茶落季 2020-12-12 23:07

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

12条回答
  •  心在旅途
    2020-12-12 23:38

    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;
    
    ReactDOM.render(
      
    React version: {REACT_VERSION}
    , document.getElementById('root') );
    
    
    
    

    Example with React imported as a module

    import React from 'react';
    
    console.log(React.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).

提交回复
热议问题