将触发功能传递给两个组件

时间:2016-06-17 22:05:45

标签: javascript reactjs

这是我想要做的:

render() {
    return (
        <div>
            <Child onTrigger={xxx} />

            <button onClick={xxx} />
        </div>
    )
}

点击button后,我想在Child中发生一些事情。我该怎么做呢?它不能只是一个布尔值,因为它应该是一个触发器并被多次调用。

1 个答案:

答案 0 :(得分:2)

你有两个选择

1º将计数器添加到您的父状态,并在每次单击按钮时将其递增。

2º为您的孩子添加一个参考并使用(000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> Connected on port 2222, sending welcome message... (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> 220-FileZilla Server 0.9.57 beta (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> 220-written by Tim Kosse (tim.kosse@filezilla-project.org) (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> 220 Please visit https://filezilla-project.org/ (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> FEAT (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> 211-Features: (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> MDTM (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> REST STREAM (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> SIZE (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> MLST type*;size*;modify*; (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> MLSD (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> AUTH SSL (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> AUTH TLS (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> PROT (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> PBSZ (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> UTF8 (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> CLNT (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> MFMT (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> EPSV (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> EPRT (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> 211 End (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> CLNT lftp/4.0.9 (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> 200 Don't care (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> OPTS UTF8 ON (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> 202 UTF8 mode is always enabled. No need to send this command. (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> USER emre** (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> 331 Password required for emre** (000017)6/17/2016 19:01:11 PM - (not logged in) (***.***.***.**)> PASS ******** (000017)6/17/2016 19:01:11 PM - emre** (***.***.***.**)> 230 Logged on (000017)6/17/2016 19:01:11 PM - emre** (***.***.***.**)> PWD (000017)6/17/2016 19:01:11 PM - emre** (***.***.***.**)> 257 "/" is current directory. (000017)6/17/2016 19:01:11 PM - emre** (***.***.***.**)> PASV (000017)6/17/2016 19:01:11 PM - emre** (***.***.***.**)> 227 Entering Passive Mode (76,100,88,71,199,56) (000017)6/17/2016 19:01:11 PM - emre** (***.***.***.**)> LIST (000017)6/17/2016 19:01:11 PM - emre** (***.***.***.**)> ABOR (000017)6/17/2016 19:01:11 PM - emre** (***.***.***.**)> 226 ABOR command successful (000017)6/17/2016 19:01:11 PM - emre** (***.***.***.**)> disconnected.

触发该功能

像这样的东西

this.refs.child.yourFunctionHere()

full working example