reactjs - ReactNative: undefined is not an object (this.state.listAccounts) -
class widgets extends component { constructor(props){ super(props); this.state = { accesstoken: "", listaccounts:[], } } componentwillmount(){ this.setstate({listaccounts: this.loadaccountsdata()}) } loadaccountsdata(){ //return data server } render() { return ( <content> {this.state.listaccounts.map( (account) => <account accountdata={account} />)} </content> ) } }
above blue print of code. unabele access state in render? error: undefined not object (evaluating 'this.state.listaccounts.map')
you setting state on async request. need wait till response comes set state.
class widgets extends component { constructor(props){ super(props); this.state = { accesstoken: "", listaccounts:[], } } componentwillmount(){ this.loadaccountsdata(); } loadaccountsdata(){ somerequest().then( (response) => { this.setstate({listaccounts: response}); // or whatever data accounts }); } render() { return ( <content> {this.state.listaccounts.map( (account) => <account accountdata={account} />)} </content> ) } }
Comments
Post a Comment