scrimba
Frontend Career Path
Advanced React
Reusability
Fix onToggle bug using refs
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Fix onToggle bug using refs
AboutCommentsNotes
Fix onToggle bug using refs
Expand for more info
components
Toggle
Toggle.js
run
preview
console
import React from "react"

const ToggleContext = React.createContext()

export default function Toggle({ children, onToggle }) {
const [on, setOn] = React.useState(false)
/**
* Challenge: We only want to run onToggle() AFTER the
* first time rendering the component. We can use refs to
* track whether or not it's the first time this component
* is rendering
*
* 1. Create a ref called `firstRender` which defaults to `true`
* (Since when we first create the ref, it's true that it
* is the first time the component is rendering)
* 2. In the useEffect, check if your ref's value is `true`.
* If it is, set it to `false`. If it isn't... can you figure
* it out??
* Hint: don't forget that your boolean value will be saved
* under `firstRender.current`, not just `firstRender`!
*/

function toggle() {
setOn(prevOn => !prevOn)
}

React.useEffect(() => {
onToggle()
}, [on])

return (
<ToggleContext.Provider value={{ on, toggle }}>
{children}
</ToggleContext.Provider>
)
}

export { ToggleContext }
Console
"Menu toggled"
,
"Menu toggled"
,
"Menu toggled"
,
"Menu toggled"
,
/index.html
-3:21