0
我是一個noob,目前學習reactjs
,有人可以解釋,如何訪問一個component
內的功能,在另一個component
在reactjs
?訪問組件內部的函數?
例如:
class ComponentOne extends React.Component{
constructor(){
super()
this.handleClick=this.handleClick.bind(this)
}
handleClick(){
console.log("handling click")
}
render(){
return(
<button onClick={this.handleClick}>Click me</button>
)
}
}
// This component is in another file
import ComponentOne from './ComponentOne'
class ComponentTwo extends React.Component{
constructor(){
super()
this.handleComp=this.handleComp.bind(this)
}
handleComp(){
ComponentOne.handleClick()
}
render(){
return(
<button onClick={this.handleComp}>Click me</button>
)
}
}
這樣的事情。
你的意思是訪問屬於父項的函數嗎?還是屬於一個孩子的功能?或者與一個完全不相關的(即不在樹上)組件 – patrick
你能更精確一點,併爲你的問題添加一些代碼? :) – Crocsx
@patrick你可以給任何鏈接或三種方式的例子? – nik7