-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Doesn't work with Vite + React #243
Comments
Why is this async? |
The thing is that even without async it still doesn't work ~ I made here a StackBlitz example with a blank project. See |
I was able to get this working with vite by using the following
import React from 'react';
import whyDidYouRender from '@welldone-software/why-did-you-render';
if (process.env.NODE_ENV === 'development') {
whyDidYouRender(React, {
trackAllPureComponents: true,
});
}
import './wdyr';
import React from 'react'
.
.
. Then setting |
Uhm, I'm sorry but I still can't get it to work with
import './wdyr';
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './index.css';
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
/// <reference types="@welldone-software/why-did-you-render" />
import React from 'react';
import whyDidYouRender from '@welldone-software/why-did-you-render';
if (process.env.NODE_ENV === 'development') {
whyDidYouRender(React, {
trackAllPureComponents: true,
});
}
function App() {
return (<div>/** some code */</div>);
}
App.whyDidYouRender = true;
export default App; |
I played around a little bit with your sandbox and I, too, could not get it to work. Might be something with our configs, but what we have setup at my work seems to work with this tool. |
Hope we could find a workaround because I think that this tool can really help me in some of my projects for debugging. Anyway, I'll try to find more informations on my own about this issue. If I find anything useful, I'll comment it here. |
this also seems to work const { default: whyDidYouRender } = await import('@welldone-software/why-did-you-render');
whyDidYouRender(React, {
logOnDifferentValues: true,
trackAllPureComponents: true,
}); for purposes of keeping it a dev dependency. I am not quite sure why it does not work in the example you posted above. |
@jdnarvaez wait so does it works or not? ;O
|
It works fine for me in my local project, it does not seem to work on the sandbox that is linked above. |
and also doesn't work in my Vite projects (made with |
Doesn't work for me as well |
Solution:
wdyr.ts
|
Sorry for the late response but the solution from @anatoliykot seems to work for me so I'll close this issue ! Thanks you for your help ! |
Thanks for proposing a solution, a few questions:
|
What’s working for me is the following wdyr.ts
main.tsx
tsconfig.json
Despite using the above module/target browserlist provides a prod build time check to ensure I’m still using compatible features (aka I can’t ship code that’s using top-level await but I can use them behind a dev flag) No trace of |
Why is this marked as a solution? It imports |
... it does not include it in the production bundle
This line is statically analyzed and everything below it is omitted from the production bundle (if the condition is false), meaning This also allows a user to turn it on or off locally by flipping an environment variable for This is verified by reviewing the production bundle with and without these flags set, and also verifying the production bundle size is the exact same before and after this change. |
Damn, I meant to quote @anatoliykot's post. For his case, it does include that in PROD. And yes, yours doesn't. But in certain instances, WDYR may be initialized too late because of the dynamic import. e.g. Your answer will not work in Gatsby. |
Yeah I think the only way around that is to just leave For just vite+react I saw no difference between the two so I left it as I have it. |
I find the safest way is to simply use |
Edit: So it looks like instead we can leverage vite’s main.tsx
wdyr.ts
That said, I believe that my original suggestion still works the same since the |
import * as React from 'react'; ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render( |
where is the WDYR import? |
this actually worked. @vzaidman could we add this to the documentation? PR's welcome?
|
Hello ! I'm currently having troubles trying to use this library with my React project.
This is my
wdyr.ts
file:This is how I integrate it in my
main.tsx
(mountpoint)When I run it, there isn't any errors, but still it doesn't work.
I've also tried to add
MyComponent.whyDidYouRender = true;
but still have no effect.By the way, I'm using vite
2.7.13
and react17.0.2
.Hope you can help me with this !
The text was updated successfully, but these errors were encountered: