Next.js Discord

Discord Forum

Switching Login Pages

Answered
WhyFencePost (Ping Reply) posted this in #help-forum
Open in Discord
I am making a login from, and it has a component for page one, and a component for page 2, and a component for a loading page. I have a div button in both page components, and i need to know how to connect that button to some way to handle the processing of stuff, so that I can have it take input, validate the input, and then either bring up a popup about missing feilds or go to loading, then when in loading send a request and wait for it, and then switch to page 2 and repete with that input. I have no idea how in nextjs.

44 Replies

k, thanks
Ill prolly do a form
@WhyFencePost (Ping Reply) is there a way to use a div onclick as a submit in a form?
no, you will need a button to add onSumbit prop to trigger the form submition
k, ill look up a react forms tutorial, as this is all new to me LOL
and i think im doing it wrong
good luck
any good tutorials for me to watch?
im making a multi page signin, similar to google
its just a normal from component right???
Answer
@Coffee Coke https://react.dev/learn/responding-to-events https://react.dev/reference/react-dom/components/form idk about video tutorials, you can search them in YT
k, that kinda worked, i cant get the action to work, as i need the action function to be on the main login form, but the action is called in the child, and it fails
function Login() {
  let showStage1, showStage2, showLoading;
  let loginStage = 1;

  switch (loginStage) {
    case 0:
      showLoading = true
    case 1:
      showStage1 = true
    case 2:
      showStage2 = true;
  }

  function email(formData:any) {
    const user = formData.get("userString");
    if (user) {
      loginStage = 0;
      //validate
      //Wait as debug
      loginStage = 2;
    }    
  }

  function pw(formData:any) {
    const pw = formData.get("pwString");
    if (pw) {
      loginStage = 0;
      //validate
      //Wait as debug
      loginStage = 0;
    }
  }

  return (
    <>
      {showLoading && <LoadingPage />}
      {showStage1 && <LoginPage1 />}
      {showStage2 && <LoginPage2 />}
    </>
  );
}
and email and pw are called as:
function LoginPage1() {
  return (
    <>
      <div className="w-full h-full flex justify-center items-center md:hidden">
        <form className="w-56" action={email}>
          <div className="w-56 h-24"></div>
          <FormHeader subheader="Your gateway to bookings">Jericho Account</FormHeader>
          <div className="w-56 h-32"></div>
          <MenuInput id="userString">Username or Email</MenuInput>
          <div className="w-56 h-6"></div>
          <MenuButton size="lg" variant="main" sub="true">Next</MenuButton>
          <div className="w-56 h-6"></div>
          <FormMinorText>Or</FormMinorText>
          <div className="w-56 h-2"></div>
          <MenuButton size="lg">I forgot my login</MenuButton>
        </form>
      </div>
    </>
  );
}
use react hooks
k, but idk how to set it up, i just looked into it, but i dont see what my flow of data would be here, im sry'
@WhyFencePost (Ping Reply) k, but idk how to set it up, i just looked into it, but i dont see what my flow of data would be here, im sry'
start by learning and mastering basics before jumping into projects, read the react.dev documentation and watch a few tutorials if you want it
i got smtn
sry
but also now i have a bug:
function setData(formData:any) {
    const userStringData = formData.get("userString");
    if (userStringData){
      setUserString(userStringData);
    }
    alert(userString);
  }

this works, but it alerts the previos value of userString, not the new one
so what do i do here?
ah i saw the pitfall, is there a solution?
as in forcing a rerender?
or smtn else
const [userString, setUserString] = useState();

useEffect(() => {
setUserString("my string");
alert(userString);
}, [])
what is useEffect? ill go look it up
ah, makes sense, forces an update
learn React before touching Next.js
i am kinda, i like learning by doing, so thats why. Also i get that its frustrating for you
@Coffee Coke const [userString, setUserString] = useState(); useEffect(() => { setUserString("my string"); alert(userString); }, [])
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
how do i fix that?
function LoginPage1() {
  const [userString, setUserString] = useState();

  function setData(formData:any) {
    const userStringData = formData.get("userString");
      if (userStringData){
      useEffect(() => {
        setUserString(userStringData); 
        alert(userString);
      }, []);
    }
  }

  return (
    <>
      <div className="w-full h-full flex justify-center items-center md:hidden">
        <form className="w-56" action={setData}>
          <div className="w-56 h-24"></div>
          <FormHeader subheader="Your gateway to bookings">Jericho Account</FormHeader>
          <div className="w-56 h-32"></div>
          <MenuInput id="userString">Username or Email</MenuInput>
          <div className="w-56 h-6"></div>
          <MenuButton size="lg" variant="main" sub="true">Next</MenuButton>
          <div className="w-56 h-6"></div>
          <FormMinorText>Or</FormMinorText>
          <div className="w-56 h-2"></div>
          <MenuButton size="lg">I forgot my login</MenuButton>
        </form>
      </div>
    </>
  );
}
it is
look up
look bro, i read the guides for both react and next, and im at the stage where im trying to learn by doing as i dont full understand it
sry
k, i looked it up, its due to the iff
done, fixed it
im sry bout the frustration with working with a newbn (I feel the same when I try to help)
so now that i have that working, im sry but how would i choose/detect when to switch the screens?