# 警告：一个组件正在将不受控制的输入更改为受控制的输入。这可能是由于值从未定义更改为已定义值引起的。

- **ID:** `react/controlled-uncontrolled-input-switch`
- **领域:** react
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

输入元素的 value 属性在重新渲染期间从未定义（不受控制）变为已定义值（受控制），或反之，导致 React 意外切换模式。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| react@18.2.0 | active | — | — |
| react@16.14.0 | active | — | — |

## 解决方案

1. ```
   Initialize the state variable to an empty string instead of undefined, and always pass a string to the value prop.
   ```
2. ```
   If the value can legitimately be null/undefined, use defaultValue for the initial uncontrolled state and then switch to controlled with a key prop to remount the input.
   ```

## 无效尝试

- **Adding a default value to the input via defaultValue prop while keeping value undefined** — defaultValue is only for uncontrolled inputs; mixing it with a value prop that later becomes defined still triggers the warning. (85% 失败率)
- **Using a ternary operator to conditionally set value to undefined or a string** — This creates the exact flip-flop between undefined and defined that causes the warning. (90% 失败率)
- **Setting the input's value to an empty string by default to avoid undefined** — While this avoids the warning, it may not reflect the intended initial state (e.g., null for optional fields), leading to incorrect UX. (40% 失败率)
