scrimba
Frontend Career Path
Advanced React
Reusability
Custom Hooks Intro
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

AboutCommentsNotes
Custom Hooks Intro
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)
const firstRender = React.useRef(true)


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

React.useEffect(() => {
if (firstRender.current) {
firstRender.current = false
} else {
onToggle()
}
}, [on])

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

export { ToggleContext }
Console
/index.html
-1:55