uncontrolled to controlled switch warning
See original GitHub issueI’m just trying this library and I get the following warning:
proxyConsole.js:56 Warning: LoginForm is changing an uncontrolled input of type text to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://fb.me/react-controlled-components
Am I doing anything wrong?
import React from 'react';
import { Formik, InjectedFormikProps } from 'formik';
import Yup from 'yup';
interface IValues {
email: string;
password: string;
}
interface ILoginFormProps {
onSubmit(payload: IValues): void;
}
const LoginForm: React.StatelessComponent<
InjectedFormikProps<ILoginFormProps, IValues>
> = ({ values, handleChange, handleSubmit, errors, error, isSubmitting }) =>
<form onSubmit={handleSubmit}>
<input
type="text"
name="email"
value={values.email}
onChange={handleChange}
placeholder="john@apple.com"
/>
{errors.email &&
<div>
{errors.email}
</div>}
<input
type="password"
name="password"
value={values.password}
onChange={handleChange}
placeholder="password"
/>
{errors.password &&
<div>
{errors.password}
</div>}
{error &&
error.message &&
<div style={{ color: 'red' }}>
Top Level Error: {error.message}
</div>}
<button type="submit" disabled={isSubmitting}>
Submit
</button>
</form>;
export default Formik<ILoginFormProps, IValues, IValues>({
validationSchema: Yup.object().shape({
email: Yup.string().email().required(),
password: Yup.string().required()
}),
handleSubmit: (payload, { props, setError, setSubmitting }) => {
props.onSubmit(payload);
}
})(LoginForm);
Issue Analytics
- State:
- Created 6 years ago
- Reactions:17
- Comments:21 (5 by maintainers)
Top Results From Across the Web
Warning Controlled Component Changing to Uncontrolled
Warning: A component is changing a controlled input of type hidden to be uncontrolled. Input elements should not switch from controlled to ......
Read more >A component is changing an uncontrolled input to be controlled
In this tutorial, we will learn why this warning occurs and how to solve it. Consider the following component: App.js.
Read more >Controlled Input switch warning - Material Design for Bootstrap
Receive Console Warning: A component is changing an uncontrolled input of type text to be controlled. Input elements should not switch from uncontrolled...
Read more >a component is changing a controlled input to be uncontrolled ...
Switch to Private. FAQ. Safe Search: Moderate ... This causes the controlled to uncontrolled input warning. The other issue is not correctly shallow...
Read more >Uncontrolled Components - React
In most cases, we recommend using controlled components to implement forms. In a controlled component, form data is handled by a React component....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You’re never instantiating your
values.email
andvalues.password
, so yourinput
s are receiving undefined values, which React interprets to mean the inputs are uncontrolled.I would suggest implementing (in your
Formik
implementation) themapPropsToValues
method to ensure thatemail
andpassword
are initiated to empty strings instead of undefined, for example:(Of course, if you wanted to ensure the form populated with values based on your props, you would do it there as well. I’m setting to empty strings here because your example is a login form, where you want the fields to start empty.)
@balinabbb I totally agree. I’m surprised this hasn’t been addressed with how popular this form library is. This requirement of setting a default value means that your forms have to be incredibly verbose, essentially re-defining your schema on the front-end in a format just for Formik to use. I feel like adding the inputs with the correct names should be enough. And then once you do that, it now attaches a bunch of empty values to your data that gets sent to the API. I basically have to have a method that I pass my data into which gets it ready for formik, and then a second method on submit which cleans all the garbage up. This is tedious.