5

I am getting this error and I don;t know what else to do.

I am using next.js and my code looks like this.

The _app.js:

import '../styles/globals.scss'
import React from 'react'
import Layout from '../components/Layout'
import Head from "next/head";
import Signin from "./signin";
import Register from "./register";
import { DataProvider } from "../store/GlobalState";


function MyApp ({
                    Component,
                    pageProps
                }) {

    if (typeof window !== 'undefined') {
        if (window.location.pathname === '/signin') {
            return (
                <DataProvider>
                    <Signin/>
                </DataProvider>
            )
        } else if (window.location.pathname === '/register') {
            return (
                <DataProvider>
                    <Register/>
                </DataProvider>
            )
        }
    }
    return (
            <DataProvider>
                <Head>
                    <title>Above the Sky</title>
                </Head>
                <Layout>
                    <Component {...pageProps} />
                </Layout>
            </DataProvider>
    )
}

export default MyApp

I am doing this because I want the register and the login pages to be separate from the layout, not having any header or footer whatsoever... If you have a hint on this , how I should do this better please tell me .... but this is not the main problem..

and now the Register.js:

import Head from 'next/head'
import { useContext, useEffect, useState } from "react";
import Link from 'next/link'
import valid from '../utils/valid'
import { DataContext } from "../store/GlobalState";


const Register = () => {
    const [ mounted, setMounted ] = useState(false);
    const initialState = {
        email: '',
        password: '',
        cf_password: ''
    };
    const [ userData, setUserData ] = useState(initialState);
    const {
              email,
              password,
              cf_password
          } = userData;

    const {
              state,
              dispatch
          } = useContext(DataContext)

    const handleChangeInput = e => {
        const {
                  name,
                  value
              } = e.target
        setUserData({
            ...userData,
            [name]: value
        })
        dispatch({
            type: 'NOTIFY',
            payload: {}
        })
    }

    const handleSubmit = async e => {
        e.preventDefault()
        const errorMessage = valid(email, password, cf_password)

        if (errorMessage) {
            return dispatch({
                type: 'NOTIFY',
                payload: { error: errorMessage }
            })
        }
        dispatch({
            type: 'NOTIFY',
            payload: { success: 'Ok' }
        })
    }

    useEffect(() => {
        setMounted(true)
    }, [])


    return (
        mounted
        &&

        <div style={{
            backgroundColor: 'black',
            height: '100vh'
        }}>
            <Head>
                <title>Register Page</title>
            </Head>
            <div className="login-dark" style={{ height: "695px" }}>
                <form className='container' onSubmit={handleSubmit}>
                    <div className="illustration"><i className="fas fa-thin fa-user-plus"/></div>
                    <div className="mb-3">
                        <label htmlFor="exampleInputEmail1" className="form-label">Email address</label>
                        <input type="email" className="form-control" id="exampleInputEmail1" aria-describedby="emailHelp"
                               name="email" value={email} onChange={handleChangeInput}/>
                        <div id="emailHelp" className="form-text">We'll never share your email with anyone else.</div>
                    </div>
                    <div className="mb-3">
                        <label htmlFor="exampleInputPassword1" className="form-label">Password</label>
                        <input type="password" className="form-control" id="exampleInputPassword1"
                               name="password" value={password} onChange={handleChangeInput}/>
                    </div>
                    <div className="mb-3">
                        <label htmlFor="exampleInputPassword2" className="form-label">Confirm Password</label>
                        <input type="password" className="form-control" id="exampleInputPassword2"
                               name="cf_password" value={cf_password} onChange={handleChangeInput}/>
                    </div>
                    <div className='button-container'>
                        <button type="submit" className="btn btn-primary btn-block">Register</button>
                    </div>
                    <a className="forgot" href="#">Forgot your email or password?</a>
                    <p className="have-account">Already have an account ? &nbsp;&nbsp; <Link href="/signin"><a style={{ color: 'crimson' }}>Login here</a></Link></p>
                </form>
            </div>
        </div>
    )
}

export default Register

When I render the register page I get this error in the console .. "next-dev.js?3515:32 Warning: Did not expect server HTML to contain a in ."

These are my store files aswell:

Actions.js

export const ACTIONS = {
    NOTIFY: 'NOTIFY',
    AUTH: 'AUTH'
}

Reducer.js

import { ACTIONS } from './Actions';

const reducers = (state, action) => {
    switch (action.type) {
        case ACTIONS.NOTIFY:
            return {
                ...state,
                notify: action.payload
            };
        case ACTIONS.AUTH:
            return {
                ...state,
                auth: action.payload
            };
        default:
            return state;
    }
}


export default reducers

and the GlobalState.js

import { createContext, useReducer } from "react";
import reducers from "./Reducers";


export const DataContext = createContext()

export const DataProvider = ({ children }) => {
    const initialState = {
        notify: {},
        auth: {}
    }
    const [ state, dispatch ] = useReducer(reducers, initialState)
    const { cart, auth } = state

    return (
        <DataContext.Provider value={{
            state,
            dispatch
        }}>
            {children}
        </DataContext.Provider>
    )
}
traumcode
  • 53
  • 1
  • 6
  • you can go through existing answer on so - https://stackoverflow.com/a/45371427/6426569 – MukulSharma Jan 24 '22 at 07:14
  • yeah , I saw this post but I cannot figure it out ... O_o – traumcode Jan 24 '22 at 07:21
  • please anyone could explain this to me ? I really don;t understand what I found .... :\ – traumcode Jan 24 '22 at 09:56
  • 1
    The issue occurs because you're using `typeof window !== 'undefined'` as a condition to render. This returns different things in the server and client, meaning there will be a mismatch when rehydration occurs on the client. I'd recommend you use [`next/router`](https://nextjs.org/docs/api-reference/next/router) to check for the current path, rather than using `window.location.pathname`, avoiding using the `typeof window !== 'undefined'` check entirely. – juliomalves Jan 24 '22 at 16:26
  • Also, you should probably conditionally render the `Layout` rather than the `signin` and `register` pages themselves. You can use the per-page layout pattern to do that: https://nextjs.org/docs/basic-features/layouts#per-page-layouts. – juliomalves Jan 24 '22 at 16:32
  • this is most likely caused by some chrome extension. Open in incognito and see if error dissapears – cikatomo Jun 04 '22 at 07:20

0 Answers0