Unverified Commit f56e5c94 authored by Max Proske's avatar Max Proske Committed by GitHub

Convert `with-why-did-you-render` example to TypeScript (#43736)

parent 783a3ced
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
This is a simple example of how to use [why-did-you-render](https://github.com/welldone-software/why-did-you-render) within a Next.js app. This is a simple example of how to use [why-did-you-render](https://github.com/welldone-software/why-did-you-render) within a Next.js app.
We are essentially extending webpack config to allow the monkey patched `React` version of WDYR in development mode and adding to our application We are essentially extending webpack config to allow the monkey patched `React` version of WDYR in development mode and adding to our application
by importing `wdyr.js` at the top of Next.js `_app.js`. by importing `wdyr.ts` at the top of Next.js `_app.tsx`.
By default, all pure components will be tracked, but you can add By default, all pure components will be tracked, but you can add
`Component.whyDidYouRender = true` to regular function components in case you need. `Component.whyDidYouRender = true` to regular function components in case you need.
......
const path = require('path') const path = require('path')
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
module.exports = { const nextConfig = {
webpack(config, { dev, isServer }) { webpack(config, { dev, isServer }) {
if (dev && !isServer) { if (dev && !isServer) {
const originalEntry = config.entry const originalEntry = config.entry
config.entry = async () => { config.entry = async () => {
const wdrPath = path.resolve(__dirname, './scripts/wdyr.js') const wdrPath = path.resolve(__dirname, './scripts/wdyr.ts')
const entries = await originalEntry() const entries = await originalEntry()
if (entries['main.js'] && !entries['main.js'].includes(wdrPath)) { if (entries['main.js'] && !entries['main.js'].includes(wdrPath)) {
...@@ -19,3 +19,5 @@ module.exports = { ...@@ -19,3 +19,5 @@ module.exports = {
return config return config
}, },
} }
module.exports = nextConfig
...@@ -11,6 +11,10 @@ ...@@ -11,6 +11,10 @@
"react-dom": "^18.2.0" "react-dom": "^18.2.0"
}, },
"devDependencies": { "devDependencies": {
"@welldone-software/why-did-you-render": "^7.0.1" "@types/node": "^18.11.10",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.9",
"@welldone-software/why-did-you-render": "^7.0.1",
"typescript": "^4.9.3"
} }
} }
import { AppProps } from 'next/app'
import '../scripts/wdyr' import '../scripts/wdyr'
export default function App({ Component, pageProps }) { export default function App({ Component, pageProps }: AppProps) {
return <Component {...pageProps} /> return <Component {...pageProps} />
} }
import Header from '../components/header.js' import Header from '../components/header'
export default function Home() { export default function Home() {
return ( return (
......
/// <reference types="@welldone-software/why-did-you-render" />
import React from 'react' import React from 'react'
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
......
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment