Next.js Discord

Discord Forum

TypeError: Cannot read properties of null (reading 'useState')

Answered
Singapura posted this in #help-forum
Open in Discord
SingapuraOP
src/pages/api/auth/credentials-signin.tsx
import { useState } from 'react';
import { signIn } from 'next-auth/react';

export default function SignIn() {
  const [license, setLicense] = useState('');
  const [signature, setSignature] = useState('');

  const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    signIn('credentials', { license, signature });
  };

  return (
    <form onSubmit={handleSubmit}>
      <div>
        <label htmlFor="license" className="block text-sm font-medium leading-6 text-gray-900">
          License
        </label>
        <div className="mt-2">
          <input
            type="text"
            name="license"
            id="license"
            className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
            placeholder="Enter your license"
            value={license}
            onChange={(e) => setLicense(e.target.value)}
          />
        </div>
      </div>
      <div>
        <label htmlFor="signature" className="block text-sm font-medium leading-6 text-gray-900">
          Signature
        </label>
        <div className="mt-2">
          <input
            type="password"
            name="signature"
            id="signature"
            className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
            placeholder="Enter your signature"
            value={signature}
            onChange={(e) => setSignature(e.target.value)}
          />
        </div>
      </div>
      <button type="submit">Sign in</button>
    </form>
  );
}
Answered by Ray
page component shouldn't be put inside api folder. can you move it to somewhere else?
View full answer

12 Replies

SingapuraOP
I am using the stock t3 app with next auth. However, when trying to setup a custom login page I get an error regarding the useState implementation. Can anyone tell me why this may be happening. I tried all I could think of, looked on stackoverflow but nothing resolves the issue.

Any help would be greatly appreciated.
page component shouldn't be put inside api folder. can you move it to somewhere else?
Answer
anywhere under pages but not inside api which is for creating api route
like pages/credentials-signin or pages/auth/credentials-signin
SingapuraOP
Hi Ray, thank you for your prompt help with the issue I'm facing! Is this suggestion also related to the useState error I am currently experiencing? Or is it about me having to change this for good structure?
this is the rule for nextjs page router
you need to follow it or it won't work
SingapuraOP
Thanks!
I moved it out of the api folder and now it does work
Was struggling with this for hours haha
cool please mark solutions!
doc is your friend