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