简体   繁体   English

React / MUI:如何在主 App.js 中使用钩子按钮调用其他 class

[英]React / MUI : How to call other class with hook to button in main App.js

I'm new to React/MUI.. I'm creating different components for now for a website, so the UI for now doesn't matter.我是 React/MUI 的新手。我现在正在为网站创建不同的组件,所以现在的 UI 并不重要。

I'm facing this problem while trying to do a button to link me to the Sign Up page (Doing the same for Sign In):我在尝试使用按钮将我链接到注册页面时遇到了这个问题(对登录执行相同的操作):

Error: Invalid hook call.错误:无效的挂钩调用。 Hooks can only be called inside of the body of a function component.钩子只能在 function 组件的主体内部调用。 This could happen for one of the following reasons: ...这可能是由于以下原因之一: ...

The following is my code:以下是我的代码:

SignUp.js注册.js

import React from 'react';
import Avatar from '@material-ui/core/Avatar';
import Button from '@material-ui/core/Button';
import CssBaseline from '@material-ui/core/CssBaseline';
import TextField from '@material-ui/core/TextField';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Checkbox from '@material-ui/core/Checkbox';
import Link from '@material-ui/core/Link';
import Grid from '@material-ui/core/Grid';
import Box from '@material-ui/core/Box';
import LockOutlinedIcon from '@material-ui/icons/LockOutlined';
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/core/styles';
import Container from '@material-ui/core/Container';

const useStyles = makeStyles((theme) => ({
    paper: {
        marginTop: theme.spacing(8),
        display: 'flex',
        flexDirection: 'column',
        alignItems: 'center',
    },
    avatar: {
        margin: theme.spacing(1),
        backgroundColor: theme.palette.secondary.main,
    },
    form: {
        width: '100%', // Fix IE 11 issue.
        marginTop: theme.spacing(3),
    },
    submit: {
        margin: theme.spacing(3, 0, 2),
    },
}));

export default function SignUp() {
    const classes = useStyles();

    return (
        <Container component="main" maxWidth="xs">
            <CssBaseline />
            <div className={classes.paper}>
                <Avatar className={classes.avatar}>
                    <LockOutlinedIcon />
                </Avatar>
                <Typography component="h1" variant="h5">
                    Sign up
        </Typography>
                <form className={classes.form} noValidate>
                    <Grid container spacing={2}>
                        <Grid item xs={12} sm={6}>
                            <TextField
                                autoComplete="fname"
                                name="firstName"
                                variant="outlined"
                                required
                                fullWidth
                                id="firstName"
                                label="First Name"
                                autoFocus
                            />
                        </Grid>
                        <Grid item xs={12} sm={6}>
                            <TextField
                                variant="outlined"
                                required
                                fullWidth
                                id="lastName"
                                label="Last Name"
                                name="lastName"
                                autoComplete="lname"
                            />
                        </Grid>
                        <Grid item xs={12}>
                            <TextField
                                variant="outlined"
                                required
                                fullWidth
                                id="email"
                                label="Email Address"
                                name="email"
                                autoComplete="email"
                            />
                        </Grid>
                        <Grid item xs={12}>
                            <TextField
                                variant="outlined"
                                required
                                fullWidth
                                name="password"
                                label="Password"
                                type="password"
                                id="password"
                                autoComplete="current-password"
                            />
                        </Grid>
                        <Grid item xs={12}>
                            <FormControlLabel
                                control={<Checkbox value="allowExtraEmails" color="primary" />}
                                label="I want to receive inspiration, marketing promotions and updates via email."
                            />
                        </Grid>
                    </Grid>
                    <Button
                        type="submit"
                        fullWidth
                        variant="contained"
                        color="primary"
                        className={classes.submit}
                    >
                        Sign Up
          </Button>
                    <Grid container justify="flex-end">
                        <Grid item>
                            <Link href="#" variant="body2">
                                Already have an account? Sign in
              </Link>
                        </Grid>
                    </Grid>
                </form>
            </div>
            <Box mt={5}>
                <Copyright />
            </Box>
        </Container>
    );
}

App.js应用程序.js

import React from 'react';
import Container from '@material-ui/core/Container';
import Typography from '@material-ui/core/Typography';
import SignUp from './sign-up/SignUp.js';
import SignIn from './sign-in/SignIn.js';
import Box from '@material-ui/core/Box';
import Link from '@material-ui/core/Link';
import { Button, Paper } from '@material-ui/core';

export default function App() {
    return (    
        <div>
            <Container maxWidth="sm">
                <Box my={4}>
                    <Typography variant="h4" component="h1" gutterBottom>
                        Creating a website!
            </Typography>
                    <ProTip />
                    <button onClick={SignUp}>
                        Click me!
                    </button>
                    <SignIn />
                    <Copyright />
                </Box>
                </Container>
        </div>
    );
}

How do I properly convert in this case?在这种情况下如何正确转换?

You're trying to call a React component as an event handler:您正在尝试将 React 组件调用为事件处理程序:

<button onClick={SignUp}>

That will not work.那不管用。 Instead you can for example create a flag, set it to true in the onClick handler and then render SignUp based on this flag.相反,您可以例如创建一个标志,在onClick处理程序中将其设置为 true,然后根据此标志呈现SignUp

const [showSignUp, setShowSignUp]= React.useState(false)
... 
<button onClick={()=>setShowSignUp(s=>!s)}>Click me!</button>
{showSignUp ? <SignUp /> : <SignIn />}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在App.js类中设置rootview反应本机 - How to set rootview in App.js class react native 关于 app.js 主类/函数的 React-native 问题 - React-native question about app.js main class/function React 将输入从搜索组件传递到主 App.js - React passing input from search component to main App.js 如何将 boolean 值从一个反应组件设置为 false 到主 app.js - how do a set a boolean value to false from one react component to main app.js AngularJS,ocLazyLoad和ui-router:如何在主app.js类没有集中&#39;state&#39;的情况下按需加载 - AngularJS, ocLazyLoad and ui-router: How to load on demand without centralize 'state' on the main app.js class React Native:如何将 props 变量从其他 js 文件传递到 app.js 文件 - React Native: How to pass a props variable from other js file to app.js file 在来自 App.js 的 React js 如何调用另一个 javascript 文件(服务)方法来使用 API 调用 - In React js from App.js how to call another javascript file (service) method to consume API calls 如何从 React JS 的公共文件夹中的 App.js 文件调用 function - How to call function from App.js file inside public folder in React JS React hook:如何通过类中的多个按钮调用? - React hook : How to call through multiple button inside a Class? 如何从另外两个组件中调用App.js中的事件处理函数 - How to call an event handler function from App.js in two other components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM