我有一个具有组件的反应应用程序:
从’react’导入React,{Component};import’../css/TrelloCards.css’;
TrelloCards类扩展了Component { componentDidMount(){ const …
在你的情况下 render 没有访问权限 cards 您通过trello下载(它们只能在内部访问) componentDidMount )。解决此问题的一种方法是将下载的卡保存到反应状态。 render 然后将被调用,因为状态已更改并且将呈现卡片。
render
cards
componentDidMount
class TrelloCards extends Component { constructor() { this.state = { cards: [] <-------- define your initial state } } componentDidMount() { const trello = window.Trello; trello.authorize({ type: 'redirect', name: 'React Trello', scope: { read: 'true', write: 'true' }, expiration: 'never', success: () => console.log('auth success'), error: () => console.log('auth failure'), response_type: 'token', }); trello.get('/member/me/cards', (cards) => this.setState({ cards }), ^---- set state here (shorthand) (error) => console.log('could not get cards')) } } render() { return( <div> {this.state.cards.map(card => <li>{card.name}</li>)} ^---- render cards from state </div> ); } }