我有以下組件:
const testLog = () => {
console.log('aaa - test') ;
}
const MyComponent = () => (
<img onLoad={() => testLog()} />
);
然后我在我的主應用程式中參考該組件:
<MyComponent />
我可以看到該組件肯定出現在螢屏上 - 我可以看到影像組件,但是,我遇到的問題是日志訊息永遠不會觸發。
起初,我以為我可能需要系結它,所以嘗試了這個:
const MyComponent = () => (
<img onLoad={() => this.testLog.bind(this)} />
);
但它只是告訴我testLog沒有使用。
uj5u.com熱心網友回復:
為了onLoad開火。您需要 img 才能擁有 src。查看您提供的代碼,img 標記沒有要加載的內容。
uj5u.com熱心網友回復:
嘗試使用 useEffect 鉤子,請 https://reactjs.org/docs/hooks-effect.html
useEffect(()=>{
// TODO: your code here
this.testLog.bind(this)
},[]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/523540.html
下一篇:閃亮的應用程式無法下載格子圖
