美文网首页
组件通信- 父子/爷孙

组件通信- 父子/爷孙

作者: sweetBoy_9126 | 来源:发表于2019-04-10 19:05 被阅读0次

下面通过一个龟兔赛跑案例来帮助理解组件通信
特别说明:
如果组件不需要内部状态就用函数,否则就用class
class组件里的jsx中使用函数必须绑定this,函数组件不需要

  1. 简单的龟兔显示
.header{
  display: flex;
  justify-content: center;
}
.track{
  border-bottom: 1px solid black;
}
function App(){
  return (
    <div>
      <div class="header">
        <Time1/>
        <Judge/>
        <Time2/>
      </div>
      <Track1/>
      <Track2/>
    </div>
  )
}
function Time1(){
  return (
    <div>
      <h2>🐇用时</h2>
      <div>0</div>
    </div>
  )
}
function Time2(){
  return (
    <div>
      <h2>🐢用时</h2>
      <div>0</div>
    </div>
  )
}
function Judge(){
  return (
    <div>裁判</div>
  )
}
function Track1(){
  return (
    <div>
      <div>🐇</div>
      <div class="track"></div>
    </div>
  )
}
function Track2(){
  return (
    <div>
      <div>🐢</div>
      <div class="track"></div>
    </div>
  )
}
ReactDOM.render(
  <App />,
  document.querySelector('#root')
)
  1. 实现龟兔的移动
    思路:我们需要通过transform:translateX()通过改变百分比来实现他们的移动,这里因为他们的速度要不一样所以需要给这两个分别设置一个自己的属性来控制百分比,因此我们需要使用class
class Track1 extends React.Component{
  constructor(props){
    super(props)
    let n = 0
    this.state = {
      style: {
        transform: `translateX(${n}%)`
      }
    }
    this.timerId = setInterval(()=>{
      n += 10
      this.setState({
        style: {
          transform: `translateX(${n}%)`
        }
      })
      if(n >= 100){
        window.clearInterval(this.timerId)
      }
    },500)
  }
  render(){
    return (
      <div>
        <div style={this.state.style} class="player">🐇</div>
        <div class="track"></div>
      </div>
    )
  }
}

class Track2 extends React.Component{
  constructor(props){
    super(props)
    let n = 0
    this.state = {
      style: {
        transform: `translateX(${n}%)`
      }
    }
    this.timerId = setInterval(()=>{
      n += 5
      this.setState({
        style: {
          transform: `translateX(${n}%)`
        }
      })
      if(n >= 100){
        window.clearInterval(this.timerId)
      }
    },500)
  }
  render(){
    return (
      <div>
        <div style={this.state.style} class="player">🐢</div>
        <div class="track"></div>
      </div>
    )
  }
}

react中动态绑定style就是需要你在state中直接写一个对象,上面的代码可以实现龟兔的移动但是还没办法通知外面你结束了从而外面拿到你的时间,那么我们该如何从外面拿到你结束的时间哪?

  1. 拿到龟兔的时间
    父子组件通信:父元素传一个函数给子元素,子元素在恰当的时间调用这个函数
class App extends React.Component{
  constructor(props){
    super(props)
    this.state = {
      t1: 0,
      t2: 0
    }
    this.start = new Date()
  }
  success1(inf){
    let newT = new Date() - this.start
    this.setState({
      t1: newT
    })
    console.log(inf)
  }
  success2(inf){
    let newT = new Date() - this.start
    this.setState({
      t2: newT
    })
    console.log(inf)
  }
  render(){
    return (
      <div>
        <div class="header">
          <Time1 result={this.state.t1}/>
          <Time2 result={this.state.t2}/>
        </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>
  )
}
class Track1 extends React.Component{
  constructor(props){
    super(props)
    let n = 0
    this.state = {
      style: {
        transform: `translateX(${n}%)`
      }
    }
    this.timerId = setInterval(()=>{
      n += 10
      this.setState({
        style: {
          transform: `translateX(${n}%)`
        }
      })
      if(n >= 100){
        window.clearInterval(this.timerId)
        props.success('兔子')
      }
    },500)
  }
  render(){
    return (
      <div>
        <div style={this.state.style} class="player">🐇</div>
        <div class="track"></div>
      </div>
    )
  }
}

class Track2 extends React.Component{
  constructor(props){
    super(props)
    let n = 0
    this.state = {
      style: {
        transform: `translateX(${n}%)`
      }
    }
    this.timerId = setInterval(()=>{
      n += 5
      this.setState({
        style: {
          transform: `translateX(${n}%)`
        }
      })
      if(n >= 100){
        window.clearInterval(this.timerId)
        props.success('乌龟')
      }
    },500)
  }
  render(){
    return (
      <div>
        <div style={this.state.style} class="player">🐢</div>
        <div class="track"></div>
      </div>
    )
  }
}

ReactDOM.render(
  <App />,
  document.querySelector('#root')
)

爷孙组件通信:爷爷把函数传给爸爸,爸爸再把他传给儿子

  1. 将跑道封装到一个单独的组件中,然后通过爷孙组件通信拿到时间
class App extends React.Component{
  render(){
    return (
      <div>
        <div class="header">
          <Time1 result={this.state.t1}/>
          <Time2 result={this.state.t2}/>
        </div>
        <Playground success1={this.success1.bind(this)} success2={this.success2.bind(this)}/>
      </div>
    )
  }
}
function Playground(props){
  return (
    <div class="playground">
      <Track1 success={props.success1}/>
      <Track2 success={props.success2}/>
    </div>
  )
}

相关文章

  • Vue实现组件间通信

    父子组件通信on('xxx',function(){}) 爷孙组件通信eventbusvar eventBus =...

  • Vue如何实现组件通信?

    Vue组件通信的三种情况: 父子通信 爷孙通信 兄弟通信 父子通信:父组件使用Prop向子组件传递数据,子组件通过...

  • 组件通信- 父子/爷孙

    下面通过一个龟兔赛跑案例来帮助理解组件通信特别说明:如果组件不需要内部状态就用函数,否则就用classclass组...

  • Vue3全局组件通信之provide / inject

    顾名思义,爷孙组件是比 父子组件通信[https://vue3.chengpeiquan.com/communic...

  • react 组件通信

    概述 react中的组件通信问题,根据层级关系总共有四种类型的组件通信:父子组件、爷孙组件、兄弟组件和任意组件。前...

  • Vue3全局组件通信之EventBus

    全局组件通信 全局组件通信是指,两个任意的组件,不管是否有关联(e.g. 父子、爷孙)的组件,都可以直接进行交流的...

  • React入门 5:组件通信 - 任意组件通信

    本篇文章内容包括: 任意两个组件之间如何通信 发布订阅模式 Redux 1. 回顾父子/爷孙组件通信 任何一个函数...

  • React02-组件通信

    React父子组件之间如何通信父组件传一个函数给子组件,子组件在适当的时候调用这个函数React爷孙组件之间如何通...

  • vue中的组件通信

    一、组件通信(组件传值) 1.1父子组件通信 1.2子父组件通信 1.3非父子组件通信(兄弟组件通信)

  • 2. 组件通信(父子/爷孙)

    今天我们通过「龟兔赛跑」的故事,来理解React中的组件通讯。 如上图,是组件之间的结构图,相当于一棵「树结构」。...

网友评论

      本文标题:组件通信- 父子/爷孙

      本文链接:https://www.haomeiwen.com/subject/snmniqtx.html