-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add codepen examples for conditional rendering page
- Loading branch information
Showing
5 changed files
with
156 additions
and
4 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function UserGreeting(props) { | ||
return <h1>Willkommen zurück!</h1>; | ||
} | ||
|
||
function GuestGreeting(props) { | ||
return <h1>Bitte melde dich an.</h1>; | ||
} | ||
|
||
function Greeting(props) { | ||
const isLoggedIn = props.isLoggedIn; | ||
if (isLoggedIn) { | ||
return <UserGreeting />; | ||
} | ||
return <GuestGreeting />; | ||
} | ||
|
||
ReactDOM.render( | ||
// Probiere mal isLoggedIn={true} aus: | ||
<Greeting isLoggedIn={false} />, | ||
document.getElementById('root') | ||
); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
class LoginControl extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.handleLoginClick = this.handleLoginClick.bind(this); | ||
this.handleLogoutClick = this.handleLogoutClick.bind(this); | ||
this.state = {isLoggedIn: false}; | ||
} | ||
|
||
handleLoginClick() { | ||
this.setState({isLoggedIn: true}); | ||
} | ||
|
||
handleLogoutClick() { | ||
this.setState({isLoggedIn: false}); | ||
} | ||
|
||
render() { | ||
const isLoggedIn = this.state.isLoggedIn; | ||
let button; | ||
|
||
if (isLoggedIn) { | ||
button = <LogoutButton onClick={this.handleLogoutClick} />; | ||
} else { | ||
button = <LoginButton onClick={this.handleLoginClick} />; | ||
} | ||
|
||
return ( | ||
<div> | ||
<Greeting isLoggedIn={isLoggedIn} /> | ||
{button} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
function UserGreeting(props) { | ||
return <h1>Willkommen zurück!</h1>; | ||
} | ||
|
||
function GuestGreeting(props) { | ||
return <h1>Bitte melde dich an.</h1>; | ||
} | ||
|
||
function Greeting(props) { | ||
const isLoggedIn = props.isLoggedIn; | ||
if (isLoggedIn) { | ||
return <UserGreeting />; | ||
} | ||
return <GuestGreeting />; | ||
} | ||
|
||
function LoginButton(props) { | ||
return ( | ||
<button onClick={props.onClick}> | ||
Anmelden | ||
</button> | ||
); | ||
} | ||
|
||
function LogoutButton(props) { | ||
return ( | ||
<button onClick={props.onClick}> | ||
Abmelden | ||
</button> | ||
); | ||
} | ||
|
||
ReactDOM.render( | ||
<LoginControl />, | ||
document.getElementById('root') | ||
); |
19 changes: 19 additions & 0 deletions
19
examples/conditional-rendering/inline-if-else-with-logical-operator.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
function Mailbox(props) { | ||
const unreadMessages = props.unreadMessages; | ||
return ( | ||
<div> | ||
<h1>Hallo!</h1> | ||
{unreadMessages.length > 0 && | ||
<h2> | ||
Du hast {unreadMessages.length} ungelesene Nachrichten. | ||
</h2> | ||
} | ||
</div> | ||
); | ||
} | ||
|
||
const messages = ['React', 'Re: React', 'Re:Re: React']; | ||
ReactDOM.render( | ||
<Mailbox unreadMessages={messages} />, | ||
document.getElementById('root') | ||
); |
41 changes: 41 additions & 0 deletions
41
examples/conditional-rendering/preventing-component-rendering.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
function WarningBanner(props) { | ||
if (!props.warn) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className="warning"> | ||
Warnung! | ||
</div> | ||
); | ||
} | ||
|
||
class Page extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = {showWarning: true}; | ||
this.handleToggleClick = this.handleToggleClick.bind(this); | ||
} | ||
|
||
handleToggleClick() { | ||
this.setState(state => ({ | ||
showWarning: !state.showWarning | ||
})); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<WarningBanner warn={this.state.showWarning} /> | ||
<button onClick={this.handleToggleClick}> | ||
{this.state.showWarning ? 'Ausblenden' : 'Anzeigen'} | ||
</button> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
ReactDOM.render( | ||
<Page />, | ||
document.getElementById('root') | ||
); |