0

I have a login form with follwoing code

let history = useHistory();
const dispatch = useDispatch();

const [email, setEmail] = useState('');
const [password, setPassword] = useState('');

const onSubmit = e => {
    e.preventDefault();
    dispatch(auth(email, password, true));
    history.replace('/home');
};
return (
    <Fragment>
        <div className="w-full max-w-sm container mt-20 mx-auto">
            <form onSubmit={onSubmit}>
                <div className="w-full mb-5">
                    <label className="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" htmlFor="email">
                        Email
                    </label>
                    <input className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:text-gray-600" value={email} onChange={(e) => setEmail(e.target.value)} type="text" placeholder="Email" />
                </div>
                <div className="w-full  mb-5">
                    <label className="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" htmlFor="password">
                        Password
                    </label>
                    <input className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:text-gray-600" value={password} onChange={(e) => setPassword(e.target.value)} type="password" placeholder="Password" />
                </div>
                <div className="flex items-center justify-between">
                    <button className="mt-5 bg-green-400 w-full hover:bg-green-500 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
                        Login
                    </button>
                </div>
                <div className="text-center mt-4 text-gray-500"><Link to='/'>Cancel</Link></div>
            </form>
        </div>
    </Fragment>
)

the above page produce some initial values in email and password. But when I have given someting like

const [email, setEmail] = useState('xxxx');

it will work correctly. Dont know why this happens.

Result with empty string,

  • You have a placeholder prop for your inputs. They will only show up if the input is empty. With the state set to an empty string they are empty, with a initial value they are not. If *correctly* to you is to have no placeholder, simply remove the prop. – Brian Thompson May 29 '20 at 15:19
  • @BrianThompson I have added the initial page. They are not even showing the placeholder. – Lakshmipriya Mukundan May 29 '20 at 15:24
  • This email is auto populated because you have saved this in your browser earlier – Khabir May 29 '20 at 15:26
  • 2
    This email is auto populated because you have saved this in your browser earlier – Khabir May 29 '20 at 15:27
  • Have you tried to remove value props in input tags? – TopWebGhost May 29 '20 at 15:32
  • @Khabir is right. You should disable autocomplete in your form but it's not that easy. – glinda93 May 29 '20 at 15:33
  • More information here: [How do you disable browser Autocomplete on web form field / input tag?](https://stackoverflow.com/questions/2530/how-do-you-disable-browser-autocomplete-on-web-form-field-input-tag). – Ed Lucas May 29 '20 at 15:51

0 Answers0