[英]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.