react: CRA: Fast Refresh breaks embedded DevTools backend

`TypeError: undefined is not a function (near ‘…e.sub…’) (anonymous function) src/backend/index.js:26 23 | // DevTools didn’t get injected into this page (maybe b’c of the contentType). 24 | return () => {}; 25 | }

26 | const subs = [ | ^ 27 | hook.sub( 28 | ‘renderer-attached’, 29 | ({ View compiled (anonymous function) src/backend.js:179 176 | hook.emit(‘shutdown’); 177 | }); 178 | 179 | initBackend(hook, agent, window); | ^ 180 | 181 | // Setup React Native style editor if the environment supports it. 182 | if (resolveRNStyle != null || hook.resolveRNStyle != null) {` React version:

Steps To Reproduce

  1. create-react-app
  2. yarn add react-devtools
  3. in index.js add import ‘react-devtools’ on first line
  4. start react-devtools
  5. start create-react-app devserver
  6. go to localhost:3000
  7. crash

Link to code example:

add react-devtools on first line of vanilla ‘create-react-app’

The current behavior

devtools

The expected behavior

Debug-Session

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Comments: 22 (8 by maintainers)

Most upvoted comments

same here, developing chrome extension using standalone react-devtools.

image

@feimosi updated the comment

Update: For anyone struggling with this, it turns out, you have to wait for specific event of react-devtools, then call reactRefreshRuntime.injectIntoGlobalHook(contentWindow) and it will work

window.addEventListener("message", (e) => {
  if (e.data.type === "React::DevTools::getSavedPreferences") {
    console.log("react devtools::getSavedPreferences");

    //@ts-ignore
    contentWindow.$RefreshRuntime$ = reactRefreshRuntime;
    //@ts-ignore
    reactRefreshRuntime.injectIntoGlobalHook(contentWindow);
    //@ts-ignore
    console.log("this", contentWindow.$RefreshRuntime$);
    // @ts-ignore
    contentWindow.$RefreshReg$ = () => {};
    // @ts-ignore
    contentWindow.$RefreshSig$ = () => (type) => type;
  }
});

Edited for formatting by @bvaughn.

Having this problem too. e.sub is not a function

I have same issue when using react-refresh. And here is my solution(NOTICE: IT’S NOT OK FOR ALMOST EVERYONE, IT USES A VERY BAD TRICK to get the object of react-devtools-core and ALMOST NOT WORK in your enviorment, just for reference):

const HmrHook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
globalThis.window = {};
require("react-devtools-core");
const devToolsHook = globalThis.window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
globalThis.window = globalThis;
Object.defineProperty(window,'__REACT_DEVTOOLS_GLOBAL_HOOK__',{value:devToolsHook});
const fiberIdMap = new Map();
['inject','onScheduleFiberRoot','onCommitFiberRoot','onCommitFiberUnmount'].forEach((n)=>{
    if(devToolsHook[n]){
        let originalHook = devToolsHook[n];
        devToolsHook[n] = function(...args){
            let hmrResp = HmrHook[n].bind(devToolsHook)(...args);
            if( n === 'onScheduleFiberRoot' || n === 'onCommitFiberRoot'){
                args[0] = fiberIdMap.get(args[0]);
            }
            let originalResp = originalHook.bind(devToolsHook)(...args);
            if( n === 'inject' ){
                fiberIdMap.set(hmrResp,originalResp);
            }
            return hmrResp;
        }
    }else devToolsHook[n] = HmrHook[n];
})
HmrHook['renderers'].forEach = function(...args){
    return devToolsHook['renderers'].forEach(...args);
}

@mecirmartin I have not had a chance to dig into this issue, so I can’t say. But I did want to caution against depending on a solution like the one mentioned above.

@mecirmartin I later encountered some other issues so I gave up on this. When I have more time I’ll give it another try.