scrimba
Frontend Career Path
Advanced React
Reusability
Toggle Context
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
Toggle Context
Expand for more info
Toggle
Toggle.js
run
preview
console
import React from "react"

export default function Toggle({ children }) {
/**
* Challenge: set up context!
* Reminder of the steps:
* 1. Create a new context (outside the component, but in this file)
* 2. Export that context instance from the file so we
* can use it eleswhere
* 3. Use the Context Provider to wrap the `children` returned
* from this Toggle component
* 4. Pass the state values to the context value prop for access
* from child components later on
*/

const [on, setOn] = React.useState(false)

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

return children
}
Console
/index.html
-3:41