繁体   English   中英

使用由 props 传递的 function 并且与给定事件不关联作为点击

[英]Use a function that was passed by props and is not associated with a given event as a click

早上好社区,我有一个问题。 我正在使用 react js,我必须通过道具将 function 传递给子组件。 问题是这个 function 不会直接与点击相关联,但我必须使用它,但在子组件中应用一些逻辑,如果满足条件,启动由道具传递的 function。 我所说的可以完成还是总是自动通过的 function 必须与箭头 function 和给定事件相关联?

export default father extends Component{
    const fun = () => {
      console.log("hello")
    }

  render(){
   return(
    <Child fun = {this.fun} />
   )
  }

export default child extends Component{
    this.state = {
        count : 1
    }
    const thing = () => {
        const { count } = this.state
        if(count == 1){
            this.props.fun
        }else{
            console.log("is not 1")
        }
    }
    render(){
      return(
        <Button onPress = {() => this.thing()} />
     )
   }
}

这是一个非常糟糕的例子,但它类似于我想做的事情

是的,你写的都是可以的。 作为 props 传递的 function 可以有条件地执行,也可以作为事件处理程序执行,或者根本不执行。 一旦你将它作为道具传递,它完全取决于你想如何使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM