packages/codemods/src/codemods/v6.x.x/entryClientNullCheck/README.md
Description
When you enable typescript strict mode the return type of the getElementById function will be HTMLElement | null. This means that you need to check if the element exists before using it. This codemod adds a check to ensure the element exists before using it.
You will also see a clearer error message in the browser console if the element does not exist.
Examples
For example the following entry.client.tsx:
import { hydrateRoot, createRoot } from 'react-dom/client'
import App from './App'
/**
* When `#redwood-app` isn't empty then it's very likely that you're using
* prerendering. So React attaches event listeners to the existing markup
* rather than replacing it.
* https://reactjs.org/docs/react-dom-client.html#hydrateroot
*/
const redwoodAppElement = document.getElementById('redwood-app')
if (redwoodAppElement.children?.length > 0) {
hydrateRoot(redwoodAppElement, <App />)
} else {
const root = createRoot(redwoodAppElement)
root.render(<App />)
}
would become:
import { hydrateRoot, createRoot } from 'react-dom/client'
import App from './App'
/**
* When `#redwood-app` isn't empty then it's very likely that you're using
* prerendering. So React attaches event listeners to the existing markup
* rather than replacing it.
* https://reactjs.org/docs/react-dom-client.html#hydrateroot
*/
const redwoodAppElement = document.getElementById('redwood-app')
if (!redwoodAppElement) {
throw new Error(
"Could not find an element with ID 'redwood-app'. Please ensure it exists in your 'web/src/index.html' file.",
)
}
if (redwoodAppElement.children?.length > 0) {
hydrateRoot(redwoodAppElement, <App />)
} else {
const root = createRoot(redwoodAppElement)
root.render(<App />)
}
where a check to ensure the element exists has been added.