reactjs / reactjs/react.dev

Improve uncontrolled form components documentation

Đang mở
#1,126 0 bình luận 1 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ả

Referring to Uncontrolled Components I think there are some pitfalls to be documented when using them.


Rendering uncontrolled components correctly

In contrast to controlled components, uncontrolled components using defaultValue, defaultChecked should not be rendered until the default is present, respectively <select> should not be rendered until the default and the <option> children are present, because the functionality of the defaultValue or defaultChecked is only given after mounting, not on updating.

Not being rendered can be achieved in two different ways:

  1. simply don't render it e.g. return null
  2. force rerender with changing key on it

If you strictly don't want partial results in your form use "B", else "C" would work.

// A)
// bad
class UncontrolledMotd extends React.Component {

  state = {
    text: null
  };

  async componentDidMount() {
    const res = await fetch('/motd');
    const text = await res.text();
    this.setState({text});
  }

  render() {
    // bad default
    return (
      <input type="text" defaultValue={this.state.text} />
    );
  }
}
// B)
// better: no render, no placeholder
class UncontrolledMotd extends React.Component {

  state = {
    loaded: false,
    text: null
  };

  async componentDidMount() {
    const res = await fetch('/motd');
    const text = await res.text();
    this.setState({loaded: true, text});
  }

  render() {
    if (!this.state.loaded) {
      return null;
    }

    return (
      <input type="text" defaultValue={this.state.text} />
    );
  }
}
// C)
// better: placeholder, using key
class UncontrolledMotd extends React.Component {

  state = {
    loaded: false,
    text: null
  };

  async componentDidMount() {
    const res = await fetch('/motd');
    const text = await res.text();
    this.setState({loaded: true, text});
  }

  render() {
    return (
      <input key={!this.state.loaded ? 'preview' : 'final'} type="text" defaultValue={this.state.text} />
    );
  }
}

Did not test code above, but the idea in my current project and it works.
What do you think?

Regards Philipp

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 bằng cách xem lại tài liệu Uncontrolled Components được liên kết và các ví dụ được đề xuất trong issue này. Xác minh hành vi của các component không được kiểm soát và xác định cách đưa các điểm dễ mắc lỗi vào tài liệu; công việc được xem là hoàn tất khi các hướng dẫn liên quan và các ví dụ đã được kiểm thử được tích hợp một cách rõ ràng.

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ó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/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.