javascript - "this" undefined in exported react function -
problem: when exported function called react component, console.log(this)
displays undefined. expecting return component since have bound in constructor.
leaderboard.js:
import react 'react'; import {leaderboard, createleaderboard} '../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){ }
you calling showleaderboard
instead of this.showleaderboard
- 1 bound this
to.
Comments
Post a Comment