Created
August 18, 2020 02:35
-
-
Save mutukrish/02507d8e9e355e7d690f8ed2fca4c6cf to your computer and use it in GitHub Desktop.
React Component patterns
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