diff --git a/src/content/blog/2024/04/25/react-19.md b/src/content/blog/2024/04/25/react-19.md index dbcc9f143ff..52a8e1ec3d1 100644 --- a/src/content/blog/2024/04/25/react-19.md +++ b/src/content/blog/2024/04/25/react-19.md @@ -274,24 +274,24 @@ To fix, you need to pass a promise from a suspense powered library or framework -You can also read context with `use`, allowing you to read Context conditionally: +You can also read context with `use`, allowing you to read Context conditionally such as after early returns: -```js {1,8,10} +```js {1,11} import {use} from 'react'; -import LightThemeContext from './LightThemeContext' -import DarkThemeContext from './ThemeContext' - -function ThemedPage({theme, children}) { - let currentTheme; - if (theme === 'dark') { - currentTheme = use(DarkThemeContext); - } else { - currentTheme = use(LightThemeContext); - } +import ThemeContext from './ThemeContext' + +function Heading({children}) { + if (children == null) { + return null; + } + + // This would not work with useContext + // because of the early return. + const theme = use(ThemeContext); return ( - +

{children} - +

); } ```