[英]“this” undefined in exported react function
問題:當我的React組件調用導出的函數時, console.log(this)
顯示未定義。 我一直期望它返回組件,因為我已經將其綁定在構造函數中。
Leaderboard.js:
import React from 'react';
import {leaderboard, createLeaderboard} from '../utility/gamecode';
class Leaderboard extends React.Component{
constructor(props){
super(props);
this.showLeaderboard = showLeaderboard.bind(this);
this.state = {
}
};
componentDidUpdate(){
if(this.props.leaderboard){
showLeaderboard();
}
}
render(){
return(
<div className="leaderboard hidden">
</div>
)
}
}
export default Leaderboard;
gamecode.js:
export function showLeaderboard(){
console.log(this);
}
//-----------------------
export function createLeaderboard(props){
}
您呼叫showLeaderboard
而不是this.showLeaderboard
-您綁定的一個this
來。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.