scrimba
React Bootcamp Course
Render Props Part 3
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

Render Props Part 3
AboutCommentsNotes
Render Props Part 3
Expand for more info
HOCs
withToggler.js
run
preview
console
import React, {Component} from "react"
// HOC is a function that takes a component as a parameter
// and returns a new component wrapping the given component
// and "supercharging" it by giving it some extra abilities

class Toggler extends Component {
state = {
on: this.props.defaultOnValue
}

toggle = () => {
this.setState(prevState => {
return {
on: !prevState.on
}
})
}

render() {
const {component: C, defaultOnValue, ...props} = this.props
return (
<C on={this.state.on} toggle={this.toggle} {...props} />
)
}
}

export function withToggler(component, optionsObj) {
return function(props) {
return (
<Toggler component={component} defaultOnValue={optionsObj.defaultOnValue} {...props}/>
)
}
}
Console
/
-19:15