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

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

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

相关标签:
12条回答
  • 2020-12-12 23:36

    First Install React dev tools if not installed and then use the run below code in the browser console :

    __REACT_DEVTOOLS_GLOBAL_HOOK__.renderers.get(1).version
    
    0 讨论(0)
  • 2020-12-12 23:37

    It is not certain that any global ECMAScript variables have been exported and html/css does not necessarily indicate React. So look in the .js.

    Method 1: Look in ECMAScript:

    The version number is exported by both modules react-dom and react but those names are often removed by bundling and the version hidden inside an execution context that cannot be accessed. A clever break point may reveal the value directly, or you can search the ECMAScript:

    1. Load the Web page (you can try https://www.instagram.com they’re total Coolaiders)
    2. Open Chrome Developer Tools on Sources tab (control+shift+i or command+shift+i)
      1. Dev tools open on the Sources tab
    3. In the very right of the top menu bar, click the vertical ellipsis and select search all files
    4. In he search box down on left type FIRED in capital letters, clear the checkbox Ignore case, type Enter
      1. One or more matches appear below. The version is an export very close to the search string looking like version: "16.0.0"
    5. If the version number is not immediately visible: double click a line that begins with a line number
      1. ECMAScript appears in the middle pane
    6. If the version number is not immediately visible: click the two braces at bottom left of the ECMAScript pane {}
      1. ECMAScript is reformatted and easier to read
    7. If the version number is not immediately visible: scroll up and down a few lines to find it or try another search key
      1. If the code is not minified, search for ReactVersion There should be 2 hits with the same value
      2. If the code is minified, search for either SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED or react-dom
      3. Or search for the likely version string itself: "15. or "16. or even "0.15

    Method 2: Use a DOM breakpoint:

    1. Load the page rendered by React
    2. Right click a React element (anything, like an input field or a box) and select Inspect Element
      1. Chrome Developer Tools displays the Elements pane
    3. As high up in the tree as possible from the selected element, but no higher than the React root element (often a div directly inside body with id root: <div id="root">), right click an element and select Break On… - subtree modifications
      1. Note: It is possible to compare contents of the Elements tab (DOM current state) with the response for the same resouce on the Networks tab. This may reveal React’s root element
    4. Reload the page by clicking Reload left of the address bar
      1. Chrome Developer Tools stops at the breakpoint and displays the Sources pane
    5. In the rightmost pane, examine the Call Stack sub-pane
    6. As far down the call stack as possible, there should be a render entry, this is ReactDOM.render
    7. Click the line below render, ie. the code that invokes render
    8. The middle pane now displays ECMAScript with an expression containing .render highlighted
    9. Hover the mouse cursor over the object used to invoke render, is. the react-dom module exports object
      1. if the code line goes: Object(u.render)(…, hover over the u
    10. A tooltip window is displayed containing version: "15.6.2", ie. all values exported by react-dom

    The version is also injected into React dev tools, but as far as I know not displayed anywhere.

    0 讨论(0)
  • 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(
      <div>React version: {REACT_VERSION}</div>,
      document.getElementById('root')
    );
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    
    <div id="root"></div>

    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).

    0 讨论(0)
  • 2020-12-12 23:39

    From the command line:

    npm view react version
    npm view react-native version
    
    0 讨论(0)
  • 2020-12-12 23:39

    In an existing project a simple way to see the React version is to go to a render method of any component and add:

    <p>{React.version}</p>

    This assumes you import React like this: import React from 'react'

    0 讨论(0)
  • 2020-12-12 23:45

    Open the console, then run window.React.version.

    This worked for me in Safari and Chrome while upgrading from 0.12.2 to 16.2.0.

    0 讨论(0)
提交回复
热议问题