【问题标题】:passing props to a child functional component将道具传递给子功能组件
【发布时间】: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


    【解决方案1】:

    loginButton.js 中将 this.props.authStatus(true)} 更改为 props.authStatus(true)} 因为它是功能组件而不是基于类的组件。

    export function GoogleLoginButton({authStatus}) {
    const classes = useStyles();
        return (
                <div className={classes.root}>
                    <Button variant="contained" size="large" color="primary" 
                     onClick={() => authStatus(true)}>
                        Login with your Google Account
                    </Button>
                </div>
            );
      }
    

    你可以使用解构。

    【讨论】:

      【解决方案2】:

      您不必在函数组件中使用this.propsprops 可用作函数参数。

      仅在class 组件中,props 以this.props 的形式访问。

      export function GoogleLoginButton(props) {
          const classes = useStyles();
      
          return (
              <div className={classes.root}>
                  <Button variant="contained" size="large" color="primary" onClick={() => props.authStatus(true)}>
                      Login with your Google Account
                  </Button>
              </div>
          );
      }
      

      【讨论】:

        猜你喜欢
        • 2017-02-19
        • 1970-01-01
        • 2018-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多