反应渲染 trello 卡片名称
React rendering trello card names
我有一个 React 应用程序,它有这样一个组件:
import React, { Component } from 'react';
import '../css/TrelloCards.css';
class TrelloCards extends Component {
componentDidMount() {
const authenticationFailure = () => { console.log('Auth failure') };
const trello = window.Trello;
const getCards = () => {
const error = (error) => {
console.log(error);
}
const cards = (cards) => {
console.log(cards);
}
trello.get('/member/me/cards', cards, error);
}
trello.authorize({
type: 'redirect',
name: 'React Trello',
scope: {
read: 'true',
write: 'true' },
expiration: 'never',
success: getCards,
error: authenticationFailure,
response_type: 'token',
});
}
render() {
return(
<h1>Placeholder</h1>
);
}
}
export default TrelloCards;
我已经成功地通过控制台记录了我的卡片,但现在我想在页面上呈现它们,我试过了
render() {
return(
<ul>
{cards}
</ul>
);
}
我试过通过以下卡片进行映射:
cards.map(card => {
return(
<li>{card.name}</li>
);
}
但我收到 'cards' 未定义的错误。一般来说,我对 React 和编程还很陌生,如有任何帮助,我们将不胜感激。
在您的情况下,render
无法访问您通过 trello 下载的 cards
(它们只能在 componentDidMount
中访问)。解决这个问题的一种方法是将下载的卡片保存到反应状态。 render
然后将被调用,因为状态已更改并且将呈现卡片。
例子
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>
);
}
}
我有一个 React 应用程序,它有这样一个组件:
import React, { Component } from 'react';
import '../css/TrelloCards.css';
class TrelloCards extends Component {
componentDidMount() {
const authenticationFailure = () => { console.log('Auth failure') };
const trello = window.Trello;
const getCards = () => {
const error = (error) => {
console.log(error);
}
const cards = (cards) => {
console.log(cards);
}
trello.get('/member/me/cards', cards, error);
}
trello.authorize({
type: 'redirect',
name: 'React Trello',
scope: {
read: 'true',
write: 'true' },
expiration: 'never',
success: getCards,
error: authenticationFailure,
response_type: 'token',
});
}
render() {
return(
<h1>Placeholder</h1>
);
}
}
export default TrelloCards;
我已经成功地通过控制台记录了我的卡片,但现在我想在页面上呈现它们,我试过了
render() {
return(
<ul>
{cards}
</ul>
);
}
我试过通过以下卡片进行映射:
cards.map(card => {
return(
<li>{card.name}</li>
);
}
但我收到 'cards' 未定义的错误。一般来说,我对 React 和编程还很陌生,如有任何帮助,我们将不胜感激。
在您的情况下,render
无法访问您通过 trello 下载的 cards
(它们只能在 componentDidMount
中访问)。解决这个问题的一种方法是将下载的卡片保存到反应状态。 render
然后将被调用,因为状态已更改并且将呈现卡片。
例子
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>
);
}
}