【发布时间】:2020-08-20 12:42:31
【问题描述】:
我正在尝试传递 auth 变量和一个函数来切换 true 和 false,以便我可以根据用户的状态来控制显示的内容是他是否经过身份验证
我得到这个错误,即使我认为一切都很好..我用于类组件的代码也一样,它可以工作,我可以console.log(props)在渲染函数旁边..我想我在这里遗漏了一些东西这个功能组件..我需要另一只眼睛来看看这个并告诉我哪里出错了
loginButtons.js:10 Uncaught TypeError: Cannot read property 'props' of undefined
at onClick (loginButtons.js:10)
at HTMLUnknownElement.callCallback (react-dom.development.js:188)
at Object.invokeGuardedCallbackDev (react-dom.development.js:237)
at invokeGuardedCallback (react-dom.development.js:292)
at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:306)
at executeDispatch (react-dom.development.js:389)
at executeDispatchesInOrder (react-dom.development.js:414)
at executeDispatchesAndRelease (react-dom.development.js:3278)
at executeDispatchesAndReleaseTopLevel (react-dom.development.js:3287)
at forEachAccumulated (react-dom.development.js:3259)
at runEventsInBatch (react-dom.development.js:3304)
at runExtractedPluginEventsInBatch (react-dom.development.js:3514)
at handleTopLevel (react-dom.development.js:3558)
at batchedEventUpdates$1 (react-dom.development.js:21871)
at batchedEventUpdates (react-dom.development.js:795)
at dispatchEventForLegacyPluginEventSystem (react-dom.development.js:3568)
at attemptToDispatchEvent (react-dom.development.js:4267)
at dispatchEvent (react-dom.development.js:4189)
at unstable_runWithPriority (scheduler.development.js:653)
at runWithPriority$1 (react-dom.development.js:11039)
at discreteUpdates$1 (react-dom.development.js:21887)
at discreteUpdates (react-dom.development.js:806)
at dispatchDiscreteEvent (react-dom.development.js:4168)
这是我的 home.js
import React, { Component } from "react";
import { GoogleLoginButton } from "./loginButtons";
import MenuBarApp from "./menuBarApp";
class Home extends Component {
constructor(props) {
super(props);
this.state = {
auth: true,
};
}
authStatus(status) {
this.setState({ auth: status });
}
render() {
const { auth } = this.state;
return (
<div>
{auth ? (
<MenuBarApp auth={this.state.auth} authStatus={this.authStatus.bind(this)} />
) : (
<GoogleLoginButton auth={this.state.auth} authStatus={this.authStatus.bind(this)} />
)}
</div>
);
}
}
export default Home;
这是 loginButtons.js
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Button from "@material-ui/core/Button";
export function GoogleLoginButton(props) {
const classes = useStyles();
return (
<div className={classes.root}>
<Button variant="contained" size="large" color="primary" onClick={() => this.props.authStatus(true)}>
Login with your Google Account
</Button>
</div>
);
}
const useStyles = makeStyles((theme) => ({
root: {
"& > *": {
margin: theme.spacing(1),
},
},
}));
【问题讨论】:
标签: reactjs react-props react-functional-component