Skip to content

Commit

Permalink
Update README with deferred rendering tip
Browse files Browse the repository at this point in the history
  • Loading branch information
insin committed May 5, 2020
1 parent 60aa5e3 commit 83409b5
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,26 @@ class MyComponent extends React.Component {
render() {
return (
<ThemeToggler>
{({ theme, toggleTheme }) => (
<label>
<input
type="checkbox"
onChange={e => toggleTheme(e.target.checked ? 'dark' : 'light')}
checked={theme === 'dark'}
/>{' '}
Dark mode
</label>
)}
{({ theme, toggleTheme }) => {
// Don't render anything at compile time. Deferring rendering until we
// know which theme to use on the client avoids incorrect initial
// state being displayed.
if (theme == null) {
return null
}
return (
<label>
<input
type="checkbox"
onChange={(e) =>
toggleTheme(e.target.checked ? 'dark' : 'light')
}
checked={theme === 'dark'}
/>{' '}
Dark mode
</label>
)
}}
</ThemeToggler>
)
}
Expand Down

0 comments on commit 83409b5

Please sign in to comment.