reactjs / reactjs/react.dev

Conditionally controlled component with `value` and `defaultValue` properties

Đang mở
#3,407 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
JavaScript
Star
11.8k
Fork
7.9k
Merge trung bình
1 ngày 11 giờ
Pull request đã merge (30 ngày)
11

Mô tả

In our project codebase there is an uncontrolled component that allows you to specify an initial value with value property (code examples are simplified just to show an idea):


const CustomInput = ({ value = '', onChange }) => {
  const [uncontrolledValue, setUncontrolledValue] = useState(value);

  const handleChange = (e) => {
    setUncontrolledValue(e.target.value);
    if (onChange) {
      onChange(e.target.value);
    }
  }

  return <input value={uncontrolledValue} onChange={handleChange} />
}

This component is used inside the complex form (other input components in this form are controlled). Resetting the form causes problems with this particular component because it doesn't update the internal value on props change. There is the article that recommends 2 possible solutions:

  • rewrite component to be controlled one
  • trigger force rerender of the component using key property

IMO there is another option available - make component controlled/uncontrolled depending on existence value property:


const CustomInput = ({ value: controlledValue, defaultValue = '', onChange }) => {
  const [uncontrolledValue, setUncontrolledValue] = useState(defaultValue);
  const isControlled = value !== undefined;
  const value = isControlled ? controlledValue : uncontrolledValue;

  const handleChange = (e) => {
    if (!isControlled) {
      setUncontrolledValue(e.target.value);
    }

    if (onChange) {
      onChange(e.target.value);
    }
  }

  return <input value={value} onChange={handleChange} />
}

This approach allows custom components to act as a standard <input /> element. I saw it in @kentcdodds video lesson and also in some popular libraries sources. But I can't find anything about such an approach in official react documentation. defaultValue is mentioned only here:

Should this approach be added as a recommendation in You Probably Don't Need Derived State or Forms article? If not are there any pitfalls to consider it as bad practice / anti-pattern?

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với tài liệu “You Probably Don’t Need Derived State”, Forms và Uncontrolled Components được liên kết trong issue. So sánh hướng dẫn hiện có của chúng với controlled/uncontrolled component pattern được mô tả ở đây, sau đó xác định liệu có cần thay đổi tài liệu hay không và thay đổi đó nên nằm ở đâu.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, react
Lĩnh vực
documentation
Loại issue
Tài liệu
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.