Last active
April 3, 2018 09:33
-
-
Save clucasalcantara/fdbc52cdfdc0ceb9031f50f55496c52d to your computer and use it in GitHub Desktop.
render props pattern examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import PropTypes from 'prop-types' | |
import './App.css' | |
class CounterWrapper extends Component { | |
state = { | |
count: 0, | |
} | |
increment = () => { | |
const { count } = this.state | |
return this.setState({ count: count + 1 }) | |
} | |
decrement = () => { | |
const { count } = this.state | |
return this.setState({ count: count - 1 }) | |
} | |
render() { | |
const { render } = this.props | |
const { count } = this.state | |
return ( | |
<div className="App"> | |
{ | |
render({ | |
increment: this.increment, | |
decrement: this.decrement, | |
count: count | |
}) | |
} | |
</div> | |
) | |
} | |
} | |
CounterWrapper.propTypes = { | |
render: PropTypes.func.isRequired, | |
} | |
export default CounterWrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment