Created
April 12, 2018 16:12
-
-
Save gmatheus/88966b08a45eba7575a721f7dccdd606 to your computer and use it in GitHub Desktop.
React Component Patterns: https://levelup.gitconnected.com/react-component-patterns-ab1f09be2c82
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
const Button = props => | |
<button onClick={props.onClick}> | |
{props.text} | |
</button> |
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
class ButtonCounter extends React.Component { | |
constructor() { | |
super() | |
this.state = { clicks: 0 } | |
this.handleClick = this.handleClick.bind(this) | |
} | |
handleClick() { | |
this.setState({ clicks: this.state.clicks + 1 }) | |
} | |
render() { | |
return ( | |
<Button | |
onClick={this.handleClick} | |
text={`You've clicked me ${this.state.clicks} times!`} | |
/> | |
) | |
} | |
} |
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
const UserList = props => | |
<ul> | |
{props.users.map(u => ( | |
<li>{u.name} — {u.age} years old</li> | |
))} | |
</ul> |
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
class UserListContainer extends React.Component { | |
constructor() { | |
super() | |
this.state = { users: [] } | |
} | |
componentDidMount() { | |
fetchUsers(users => this.setState({ users })) | |
} | |
render() { | |
return <UserList users={this.state.users} /> | |
} | |
} |
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
function makeToggleable(Clickable) { | |
return class extends React.Component { | |
constructor() { | |
super() | |
this.toggle = this.toggle.bind(this) | |
this.state = { show: false } | |
} | |
toggle() { | |
this.setState(prevState => ({ show: !prevState.show })) | |
} | |
render() { | |
return ( | |
<div> | |
<Clickable | |
{...this.props} | |
onClick={this.toggle} | |
/> | |
{this.state.show && this.props.children} | |
</div> | |
) | |
} | |
} | |
} |
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
@makeToggleable | |
class ToggleableMenu extends React.Component { | |
render() { | |
return ( | |
<div onClick={this.props.onClick}> | |
<h1>{this.props.title}</h1> | |
</div> | |
) | |
} | |
} |
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
class Toggleable extends React.Component { | |
constructor() { | |
super() | |
this.toggle = this.toggle.bind(this) | |
this.state = { show: false } | |
} | |
toggle() { | |
this.setState(prevState => ({ show: !prevState.show })) | |
} | |
render() { | |
return this.props.children(this.state.show, this.toggle) | |
} | |
} |
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
<Toggleable> | |
{(show, onClick) => ( | |
<div> | |
<div onClick={onClick}> | |
<h1>First Menu</h1> | |
</div> | |
{show ? | |
<p>Some content</p> | |
: null | |
} | |
</div> | |
)} | |
</Toggleable> |
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
const ToggleableMenu = props => | |
<Toggleable> | |
{(show, onClick) => ( | |
<div> | |
<div onClick={onClick}> | |
<h1>{props.title}</h1> | |
</div> | |
{show && props.children} | |
</div> | |
)} | |
</Toggleable> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
helpful to me