1.简单组件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://cdn.bootcss.com/react/16.8.1/umd/react.production.min.js"></script>
<script src="https://cdn.bootcss.com/react-dom/16.8.1/umd/react-dom.production.min.js"></script>
</head>
<body>
<script>
function App() { //简单函数组建
return (
<h1>hi</h1>
)
}
class App2 extends React.Component{ //类组件
render(){
return (
<h2>hi 2</h2>
)
}
}
ReactDOM.render(<App />, document.querySelector('#root'))
</script>
</body>
</html>
2.组件之间的通信
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>龟兔赛跑</title>
<script src="https://cdn.bootcss.com/react/16.8.1/umd/react.production.min.js"></script>
<script src="https://cdn.bootcss.com/react-dom/16.8.1/umd/react-dom.production.min.js"></script>
</head>
<body>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
padding: 40px;
}
.header{
display: flex;
justify-content: center;
}
.track{
border-bottom: 1px solid black;
}
/* .player{
transition: all 1s linear;
} */
</style>
<div id="root"></div>
<script>
// function App(){
// let t0 = new Date()
// let success1 = ()=>{
// console.log('兔子跑完了,用时:')
// console.log(new Date() - t0)
// }
// let success2 = ()=>{
// console.log('乌龟跑完了,用时:')
// console.log(new Date() - t0)
// }
// return (
// <div>
// <div class="header">
// <Time1 />
// <Time2 />
// </div>
// <Track1 success={success1} />
// <Track2 success={success2} />
// </div>
// )
// }
class App extends React.Component{
constructor(){
super()
this.state = {
result1: 0,
result2: 0
}
this.t0 = new Date()
}
success1(){
console.log('兔子跑完了,用时:')
// console.log(new Date() - this.t0)
let r1 = new Date() - this.t0
this.setState({
result1: r1
})
}
success2(){
console.log('乌龟跑完了,用时:')
// console.log(new Date() - this.t0)
let r2 = new Date() - this.t0
this.setState({
result2: r2
})
}
render(){
return (
<div>
<div class="header">
<Time1 result={this.state.result1} />
<Time2 result={this.state.result2} />
</div>
<Track1 success={this.success1.bind(this)} />
<Track2 success={this.success2.bind(this)} />
</div>
)
}
}
function Time1(props){ //兔子时间
return (
<div>
<h2>兔子用时</h2>
<div>{props.result}</div>
</div>
)
}
function Time2(props){ //乌龟时间
return (
<div>
<h2>乌龟用时</h2>
<div>{props.result}</div>
</div>
)
}
// function Track1(){ //兔子跑道
// return (
// <div>
// <div>兔子</div>
// <div class="track"></div>
// </div>
// )
// }
class Track1 extends React.Component{
constructor(){
super()
let n = 0
this.state = {
style:{
transform: `translateX(${n}%)`
}
}
let timerId = setInterval(()=>{
n+=10
this.setState({
style:{
transform:`translateX(${n}%)`
}
})
if(n>=100){
window.clearInterval(timerId)
this.props.success()
}
},1000)
}
render(){
return (
<div>
<div class="player" style={this.state.style}>兔子</div>
<div class="track"></div>
</div>
)
}
}
// function Track2(){ //乌龟跑道
// return (
// <div>
// <div>乌龟</div>
// <div class="track"></div>
// </div>
// )
// }
class Track2 extends React.Component{
constructor(){
super()
let n = 0
this.state = {
style:{
transform: `translateX(${n}%)`
}
}
let timerId = setInterval(()=>{
n+=5
this.setState({
style:{
transform:`translateX(${n}%)`
}
})
if(n>=100){
window.clearInterval(timerId)
this.props.success()
}
},1000)
}
render(){
return (
<div>
<div class="player" style={this.state.style}>乌龟</div>
<div class="track"></div>
</div>
)
}
}
ReactDOM.render(<App />,document.querySelector("#root"))
</script>
</body>
</html>
预览地址
3.变复杂一点,组件嵌套一层
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>变复杂一点</title>
<script src="https://cdn.bootcss.com/react/16.8.1/umd/react.production.min.js"></script>
<script src="https://cdn.bootcss.com/react-dom/16.8.1/umd/react-dom.production.min.js"></script>
</head>
<body>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
padding: 40px;
}
.header{
display: flex;
justify-content: center;
}
.track{
border-bottom: 1px solid black;
}
.playground{
border: 1px solid black;
background: green;
}
/* .player{
transition: all 1s linear;
} */
</style>
<div id="root"></div>
<script>
class App extends React.Component{
constructor(){
super()
this.state = {
result1: 0,
result2: 0
}
this.t0 = new Date()
}
success1(){
console.log('兔子跑完了,用时:')
// console.log(new Date() - this.t0)
let r1 = new Date() - this.t0
this.setState({
result1: r1
})
}
success2(){
console.log('乌龟跑完了,用时:')
// console.log(new Date() - this.t0)
let r2 = new Date() - this.t0
this.setState({
result2: r2
})
}
render(){
return (
<div>
<div class="header">
<Time1 result={this.state.result1} />
<Time2 result={this.state.result2} />
</div>
<Playground success1={this.success1.bind(this)}
success2={this.success2.bind(this)} />
</div>
)
}
}
function Time1(props){ //兔子时间
return (
<div>
<h2>兔子用时</h2>
<div>{props.result}</div>
</div>
)
}
function Time2(props){ //乌龟时间
return (
<div>
<h2>乌龟用时</h2>
<div>{props.result}</div>
</div>
)
}
function Playground(props){
return (
<div class="playground">
<Track1 success={props.success1}/>
<Track2 success={props.success2}/>
</div>
)
}
class Track1 extends React.Component{
constructor(){
super()
let n = 0
this.state = {
style:{
transform: `translateX(${n}%)`
}
}
let timerId = setInterval(()=>{
n+=10
this.setState({
style:{
transform:`translateX(${n}%)`
}
})
if(n>=100){
window.clearInterval(timerId)
this.props.success()
}
},1000)
}
render(){
return (
<div>
<div class="player" style={this.state.style}>兔子</div>
<div class="track"></div>
</div>
)
}
}
class Track2 extends React.Component{
constructor(){
super()
let n = 0
this.state = {
style:{
transform: `translateX(${n}%)`
}
}
let timerId = setInterval(()=>{
n+=5
this.setState({
style:{
transform:`translateX(${n}%)`
}
})
if(n>=100){
window.clearInterval(timerId)
this.props.success()
}
},1000)
}
render(){
return (
<div>
<div class="player" style={this.state.style}>乌龟</div>
<div class="track"></div>
</div>
)
}
}
ReactDOM.render(<App />,document.querySelector("#root"))
</script>
</body>
</html>
预览地址