Using a setInterval, I made the effect of changing the screen after 3 seconds to make it look like a mobile application. And when I applied this to the app.tsx, an error occurred.
Landing/index.tsx
import React, { useRef, useEffect, useState } from "react";
import * as S from "./style";
const Landing = () => {
const [sec, setSec] = useState<number>(3);
const time = useRef<number>(3);
const timerId = useRef<any>(null);
useEffect(() => {
timerId.current = setInterval(() => {
setSec(time.current % 60);
time.current -= 1;
}, 1000);
return () => clearTimeout(timerId.current);
}, []);
useEffect(() => {
if (time.current == 0) {
clearInterval(timerId.current);
}
}, [sec]);
if (time.current == 3) {
return (
<>
<S.Positioner>
<S.Wrapper>
<S.TextWrapper>
<S.Text>Ayo The</S.Text>
<S.Title>Pizza Here</S.Title>
</S.TextWrapper>
<S.Copyright>©</S.Copyright>
</S.Wrapper>
</S.Positioner>
</>
);
}
};
export default Landing;
_app.tsx
import React from "react";
import {Global} from "@emotion/react";
import {GlobalStyles} from "../style/GlobalStyle";
import Landing from "../components/Landing";
import Main from "../components/Main";
import Head from "next/head";
const MyApp = () => {
return (
<>
<Head>
<title>A-Pizza</title>
<link rel="shortcut icon" href="/favicon.ico"/>
</Head>
<Global styles={GlobalStyles}/>
<Landing/>
<Main/>
</>
)
}
export default MyApp
If erase this code, the error on the app disappears
How can I fix it?
if (time.current == 3)
return null;
fixed the error. For me all the return html code was within differentif
expressions. – Salema