<html>
<head>
<title>Document</title>
<script src="../react.js"></script>
<script src="../react-dom.js"></script>
<script src="http://cdn.bootcss.com/babel-core/5.8.38/browser.min.js"></script>
</head>
<body>
<div id="reactContainer"/>
<script type="text/babel">
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 = null
if (!isLoggedIn) {
button = <LoginButton onClick={this.handleLoginClick} />
} else {
button = <LogoutButton onClick={this.handleLogoutClick} />
}
return (
<div>
<Guest isLoggedIn={isLoggedIn} /> //can not nesting
{button}
</div>
)
}
}
function LoginButton(props) {
return (
<button onClick={props.onClick}>
Login
</button>
)
}
function LogoutButton(props) {
return (
<button onClick={props.onClick}>
Logout
</button>
)
}
function UserGreeting(props) {
return <h1>welcome back</h1>
}
function GuestGreeting(props) {
return <h1>please sign up</h1>
}
function Guest(props) {
const isLoggedIn = props.isLoggedIn
if (isLoggedIn) {
return <UserGreeting />
}
return <GuestGreeting />
}
ReactDOM.render(
<LoginControl/>,
document.getElementById('reactContainer')
)
</script>
</body>
</html>
React 按钮登入
最新推荐文章于 2024-07-18 13:01:33 发布