服务器错误ReferenceError:未定义窗口生成页面时发生此错误

时间:2020-08-18 00:00:02

标签: javascript reactjs material-ui next.js server-side-rendering

我想使用材料Ui在下一个Js中添加Microsoft登录按钮,但是 当我从NPM添加软件包并刷新页面时,我得到了上面的内容 错误。

当我如下图所示向Microsoft组件分配ClientId="a973536f-eb3e-4fd9-9394-9f4194d69153"时,出现上述错误。如何解决此错误。

import React from "react";
import { Grid, Container, Checkbox, IconButton, FormControlLabel, TextField, Button, Link } from "@material-ui/core";
import { makeStyles } from "@material-ui/core/styles";
import Icon from "@material-ui/core/Icon";
import { loadCSS } from "fg-loadcss";
import FacebookLogin from "react-facebook-login/dist/facebook-login-render-props";
import { GoogleLogin } from "react-google-login";
import MicrosoftLogin from "react-microsoft-login";

const useStyles = makeStyles((theme) => ({
    paper: {
        marginTop: theme.spacing(0),
        display: "flex",
        flexDirection: "column",
        alignItems: "center",
        height: '60vh',
    },
    background: {
        backgroundColor: "#220E1A",
        borderRadius: "5px",
        color: "white",import React from "react";


        import { Grid, Container, Checkbox, IconButton, FormControlLabel, TextField, Button, Link } from "@material-ui/core";
        
        
        import { makeStyles } from "@material-ui/core/styles";
        
        
        import Icon from "@material-ui/core/Icon";
        import { loadCSS } from "fg-loadcss";
        import FacebookLogin from "react-facebook-login/dist/facebook-login-render-props";
        import { GoogleLogin } from "react-google-login";
        
        import MicrosoftLogin from "react-microsoft-login";
        
        
        
        
        const useStyles = makeStyles((theme) => ({
            paper: {
                marginTop: theme.spacing(0),
                display: "flex",
                flexDirection: "column",
                alignItems: "center",
                height: '60vh',
        
            },
            background: {
                backgroundColor: "#220E1A",
                borderRadius: "5px",
                color: "white",
        
            },
        
            form: {
                width: "70%", // Fix IE 11 issue.
                marginTop: theme.spacing(1),
            },
            input1: {
                background: "white",
                borderRadius: "25px",
                color: "white",
        
        
            },
            submit: {
                margin: theme.spacing(1, 0, 1),
                borderRadius: "25px",
            },
            buttonGroup: {
                borderRadius: "50px",
                margin: theme.spacing(2, 0, 2, 0),
            },
            winIcon: {
                padding: '0px',
                margin: '0px',
                width: '10px'
            }
        
        }));
        
        export default function SignIn() {
            const classes = useStyles();
        
            //Load Fonts awesome icons
            React.useEffect(() => {
                const node = loadCSS(
                    "https://use.fontawesome.com/releases/v5.12.0/css/all.css",
                    document.querySelector("#font-awesome-css")
                );
        
                return () => {
                    node.parentNode.removeChild(node);
                };
            }, []);
        
        
        
            //google facebook,Microsoft Login response
            const responseFacebook = (response) => {
                console.log(response);
            };
            const responseGoogle = (response) => {
                console.log(response);
            };
        
            const authHandler = (err, data) => {
                console.log(err, data);
            };
        
        
            return (
        
                <Container maxWidth="xm" className={classes.background}>
                    <div className={classes.paper}>
                        <form className={classes.form} noValidate>
                            <TextField
                                className={classes.input1}
                                margin="normal"
                                required
                                fullWidth
                                id="email"
                                label="Email Address"
                                name="email"
                                autoFocus
                                variant="filled"
                            />
                            <TextField
                                className={classes.input1}
                                variant="filled"
                                margin="normal"
                                required
                                fullWidth
                                name="password"
                                label="Password"
                                type="password"
                                id="password"
                                autoComplete="current-password"
                            />
        
                            <Grid
                                container
                                direction="column"
                                justify="center"
                                alignItems="center"
                            >
                                <Grid item >
                                    <FormControlLabel
                                        control={<Checkbox value="remember" color="primary" />}
                                        label="Stay signed in"
                                    />
                                </Grid>
                                <Grid item>
                                    <Button
                                        type="submit"
                                        medium
                                        variant="contained"
                                        color="primary"
                                        className={classes.submit}
                                    >
                                        Sign In
                      </Button>
                                </Grid>
                                <Grid item>
                                    <Link href="#" variant="body2">
                                        Forgot password?
                      </Link>
                                </Grid>
        
                                <Grid item>
                                    <h3 align="center">Or Via</h3>{" "}
                                    <FacebookLogin
                                        appId="225241158739281"
                                        autoLoad
                                        callback={responseFacebook}
                                        render={(renderProps) => (
                                            <IconButton color="primary" onClick={renderProps.onClick}>
                                                <Icon className="fab fa-facebook" />
                                            </IconButton>
                                        )}
                                    />
                                    <GoogleLogin
                                        clientId="500452257814-peb71oi9612hv04svvfpvfrtch6pc5br.apps.googleusercontent.com"
                                        render={(renderProps) => (
                                            <IconButton
                                                onClick={renderProps.onClick} 
        
                                            >
                                                {" "}
                                                <Icon className="fab fa-google" color="primary" />
                                            </IconButton>
                                        )}
                                        buttonText="Login"
                                        onSuccess={responseGoogle}
                                        onFailure={responseGoogle}
                                        cookiePolicy={"single_host_origin"}
                                    />
        
                                    //**Problem is here IN MicrosoftLogin when i assign Id it creates the above error.**
         
                                    <MicrosoftLogin
                                        // clientId="a973536f-eb3e-4fd9-9394-9f4194d69153"
                                        authCallback={authHandler}
                                        redirectUri="https://localhost:3000/"
                                        className={classes.winIcon}
                                        children={
                                            <IconButton>
                                                <Icon className="fab fa-windows" color="primary" />
                                            </IconButton>}
                                    />
                                </Grid>
                            </Grid>
                        </form>
                    </div>
                </Container>
        
            );
        }
    },
    form: {
        width: "70%", // Fix IE 11 issue.
        marginTop: theme.spacing(1),
    },
    input1: {
        background: "white",
        borderRadius: "25px",
        color: "white",
    },
    submit: {
        margin: theme.spacing(1, 0, 1),
        borderRadius: "25px",
    },
    buttonGroup: {
        borderRadius: "50px",
        margin: theme.spacing(2, 0, 2, 0),
    },
    winIcon: {
        padding: '0px',
        margin: '0px',
        width: '10px'
    }
}));

export default function SignIn() {
    const classes = useStyles();
    //Load Fonts awesome icons
    React.useEffect(() => {
        const node = loadCSS(
            "https://use.fontawesome.com/releases/v5.12.0/css/all.css",
            document.querySelector("#font-awesome-css")
        );
        return () => {
            node.parentNode.removeChild(node);
        };
    }, []);

    //google facebook,Microsoft Login response
    const responseFacebook = (response) => {
        console.log(response);
    };
    const responseGoogle = (response) => {
        console.log(response);
    };

    const authHandler = (err, data) => {
        console.log(err, data);
    };

    return (
        <Container maxWidth="xm" className={classes.background}>
            <div className={classes.paper}>
                <form className={classes.form} noValidate>
                    <TextField
                        className={classes.input1}
                        margin="normal"
                        required
                        fullWidth
                        id="email"
                        label="Email Address"
                        name="email"
                        autoFocus
                        variant="filled"
                    />
                    <TextField
                        className={classes.input1}
                        variant="filled"
                        margin="normal"
                        required
                        fullWidth
                        name="password"
                        label="Password"
                        type="password"
                        id="password"
                        autoComplete="current-password"
                    />

                    <Grid
                        container
                        direction="column"
                        justify="center"
                        alignItems="center"
                    >
                        <Grid item >
                            <FormControlLabel
                                control={<Checkbox value="remember" color="primary" />}
                                label="Stay signed in"
                            />
                        </Grid>
                        <Grid item>
                            <Button
                                type="submit"
                                medium
                                variant="contained"
                                color="primary"
                                className={classes.submit}
                            >
                                Sign In
              </Button>
                        </Grid>
                        <Grid item>
                            <Link href="#" variant="body2">
                                Forgot password?
              </Link>
                        </Grid>

                        <Grid item>
                            <h3 align="center">Or Via</h3>{" "}
                            <FacebookLogin
                                appId="225241158739281"
                                autoLoad
                                callback={responseFacebook}
                                render={(renderProps) => (
                                    <IconButton color="primary" onClick={renderProps.onClick}>
                                        <Icon className="fab fa-facebook" />
                                    </IconButton>
                                )}
                            />
                            <GoogleLogin
                                clientId="500452257814-peb71oi9612hv04svvfpvfrtch6pc5br.apps.googleusercontent.com"
                                render={(renderProps) => (
                                    <IconButton
                                        onClick={renderProps.onClick}

                                    >
                                        {" "}
                                        <Icon className="fab fa-google" color="primary" />
                                    </IconButton>
                                )}
                                buttonText="Login"
                                onSuccess={responseGoogle}
                                onFailure={responseGoogle}
                                cookiePolicy={"single_host_origin"}
                            />

                            //**Problem is here IN MicrosoftLogin when i assign Id it creates the above error.**

                            <MicrosoftLogin
                                // clientId="a973536f-eb3e-4fd9-9394-9f4194d69153"
                                authCallback={authHandler}
                                redirectUri="https://localhost:3000/"
                                className={classes.winIcon}
                                children={
                                    <IconButton>
                                        <Icon className="fab fa-windows" color="primary" />
                                    </IconButton>}
                            />
                        </Grid>
                    </Grid>
                </form>
            </div>
        </Container>
    );
}

2 个答案:

答案 0 :(得分:1)

它与服务器端渲染和客户端渲染有关。 由于Next.js提供了SSR,因此您需要考虑使用诸如window,localStorage等对象。在编译客户端时,这些对象很好,但是当Nextjs编译服务器端时,它显示出与您共享的错误。

如果您分配客户端ID,似乎GoogleLogin使用window对象。您需要先检查一下。莱姆知道结果。

答案 1 :(得分:0)

使用Material UI组件的简单方法是在Next js框架上呈现它。只需使用component即可在客户端上呈现它。 (如果任何组件在服务器端使用Window对象,则会显示此错误)

解决方案:

    import NoSsr from '@material-ui/core/NoSsr';
                 <NoSsr>
                      <MicrosoftLogin
                        clientId="a973536f-eb3e-4fd9-9394-9f4194d69153"
                        authCallback={authHandler}
                        redirectUri="https://localhost:3000/"
                        className={classes.winIcon}
                        children={
                          <IconButton>
                            <Icon className="fab fa-windows" color="primary" />
                          </IconButton>
                        }
                      />
                    </NoSsr>