Fix Warning: A component is changing an uncontrolled input to be controlled. This is likely caused by the value changing from undefined to a defined value. in React
This warning fires when an input starts as uncontrolled (value is undefined) and later becomes controlled (value is a string). React does not support switching between the two modes. Fix it by initializing the state to an empty string instead of undefined so the input is always controlled from the first render.
Reading the Stack Trace
Here's what each line means:
- at input: The native input element transitions from uncontrolled to controlled when its value prop changes from undefined to a string.
- at RegistrationForm (webpack-internal:///./src/components/RegistrationForm.tsx:8:3): RegistrationForm initializes state with undefined or a missing property, causing the input to start as uncontrolled.
- at updateFunctionComponent (node_modules/react-dom/cjs/react-dom.development.js:19588:20): React detects the control mode switch during re-render and logs the warning.
Common Causes
1. State initialized with undefined
The form state object does not include the input field or initializes it as undefined, making the input uncontrolled on the first render.
export default function RegistrationForm() {
const [form, setForm] = useState({});
return (
<input
value={form.email}
onChange={e => setForm({ ...form, email: e.target.value })}
/>
);
}
2. Null initial value
Initializing with null instead of an empty string also causes the input to start as uncontrolled, since React treats null the same as undefined for input values.
export default function RegistrationForm() {
const [email, setEmail] = useState(null);
return <input value={email} onChange={e => setEmail(e.target.value)} />;
}
3. Conditional value prop
A ternary expression passes undefined when a condition is false, causing the input to flip between controlled and uncontrolled.
export default function RegistrationForm({ prefill }) {
const [email, setEmail] = useState(prefill?.email);
return (
<input
value={email}
onChange={e => setEmail(e.target.value)}
/>
);
}
The Fix
Initialize the form state with email set to an empty string instead of omitting it. This ensures the input is always controlled from the first render, and React never detects a transition from uncontrolled to controlled.
export default function RegistrationForm() {
const [form, setForm] = useState({});
return (
<input
value={form.email}
onChange={e => setForm({ ...form, email: e.target.value })}
/>
);
}
export default function RegistrationForm() {
const [form, setForm] = useState({ email: '' });
return (
<input
value={form.email}
onChange={e => setForm({ ...form, email: e.target.value })}
/>
);
}
Testing the Fix
import { render, screen, fireEvent } from '@testing-library/react';
import RegistrationForm from './RegistrationForm';
describe('RegistrationForm', () => {
it('renders without controlled/uncontrolled warning', () => {
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
render(<RegistrationForm />);
expect(spy).not.toHaveBeenCalledWith(
expect.stringContaining('uncontrolled input to be controlled')
);
spy.mockRestore();
});
it('starts with empty input value', () => {
render(<RegistrationForm />);
const input = screen.getByRole('textbox') as HTMLInputElement;
expect(input.value).toBe('');
});
it('updates input value on change', () => {
render(<RegistrationForm />);
const input = screen.getByRole('textbox');
fireEvent.change(input, { target: { value: 'test@email.com' } });
expect((input as HTMLInputElement).value).toBe('test@email.com');
});
});
Run your tests:
npm test
Pushing Through CI/CD
git checkout -b fix/react-form-controlled-uncontrolled,git add src/components/RegistrationForm.tsx src/components/__tests__/RegistrationForm.test.tsx,git commit -m "fix: initialize form state with empty string to keep input controlled",git push origin fix/react-form-controlled-uncontrolled
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: npm test -- --coverage
- run: npm run build
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
npm install bugstack-sdk
Step 2: Initialize
import { initBugStack } from 'bugstack-sdk'
initBugStack({ apiKey: process.env.BUGSTACK_API_KEY })
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Run the test suite locally to confirm no controlled/uncontrolled warnings.
- Open a pull request with the state initialization fix.
- Wait for CI checks to pass on the PR.
- Have a teammate review and approve the PR.
- Merge to main and verify forms work correctly in staging.
Frequently Asked Questions
BugStack validates that all form inputs maintain a consistent controlled or uncontrolled mode, runs your test suite, and checks for the warning in console output before marking it safe.
BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.
Uncontrolled inputs with useRef are fine for simple forms where you only need the value on submit. For forms with validation, conditional logic, or dynamic fields, controlled inputs give you more control.
React Hook Form uses uncontrolled inputs by default with refs, so this warning does not apply. However, switching to its Controller component makes inputs controlled and requires proper initialization.