【发布时间】:2024-05-16 18:00:02
【问题描述】:
我正在尝试使用从 Google API 导入的数据创建一个反应组件。我可以看到代码在 console.log 中运行,但是当我尝试在 React 渲染方法中使用该代码时,我什么也没得到。当我在类中移动我的函数时,它会作为未定义的函数出现。我不明白为什么?
function handleTouchTap() {
console.log('CHIP selected');
authorize();
}
function handleAccounts(response) {
console.log(response.result.username);
var username = response.result.username
console.log(username);
}
function authorize(event) {
var useImmidiate = event ? false : true;
var authData = {
client_id: CLIENT_ID,
scope: SCOPES,
immidiate: useImmidiate
};
gapi.auth.authorize(authData, function (response) {
gapi.client.load('analytics', 'v3').then(function () {
console.log(response);
gapi.client.analytics.management.accounts.list().then(handleAccounts);
});
});
}
class Chips extends React.Component {
render() {
return (
<div style={styles.wrapper}>
<Chip
onTouchTap={handleTouchTap}
style={styles.chip} >
<Avatar icon={<FontIcon className="material-icons">perm_identity</FontIcon>} />
Login
</Chip>
<Chip
style={styles.chip} >
<Avatar icon={<FontIcon className="material-icons">account_circle</FontIcon>} />
{this.username}
</Chip>
</div>
);
}
}
【问题讨论】:
-
对不起。我不习惯 *。我试图发布我的代码,但花了一些时间来了解如何去做?
-
我没有看到
onTouchTap={handleTouchTap}中引用的handleTouchTap函数 -
你能添加你得到的错误吗?
-
我上面的代码没有任何错误。我在控制台中有用户名,但我想在芯片@{this.username} 中显示用户名。它没有显示任何用户名。非常抱歉,由于我没有足够的声望点,我无法附上屏幕截图向您展示控制台。
标签: function reactjs class google-analytics components