Controlled form components with async data
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ả
This seems like a pretty common scenario when implementing forms with React+Redux but neither documentation nor any of the articles/answers online seem to provide full implementation/recommendation.
Here's the scenario
- Data is loaded initially and passed to my
<Form />component.
class Form extends React.Component {
constructor(props) {
this.state = { nameField: props.name };
}
...
}
- Initial data is rendered on the screen and state is used to track user input, making this a fully controlled component.
render() {
return(
<form>
<input value={this.state.nameField} onChange={e => this.setState({ nameField: e.target.value })} />
</form>
);
}
- Data is then saved to the server asynchronously (here I'm using Redux but the same thing can be achieved with
fetch, etc.):
handleSubmit(e) {
e.preventDefault();
const { dispatch } = this.props;
dispatch(actions.saveData(...this.state);
}
- Reducers handle successful data save and the new state is saved. This data is then once again passed to my
<Form />component as props because the redux state is my single source of truth.
I now should update my component's state to match the application state (in case there were some external changes done on the server or parent component). Since in this case "fresh" props should always overwrite the state, one would normally use componentWillReceiveProps to simply set the new state but that method is now deprecated. The other two methods available - componentDidUpdate and getDerivedStateFromProps - could work BUT there needs to be a deep comparison of new props vs old props done in order to determine if the change is due to "fresh" props or just the internal state update. This can get very expensive if my form has 30 fields because both of these methods fire on each re-render.
I am trying to figure out the best practice in this scenario - should I delegate user input changes to Redux and avoid using component's state altogether? Or the only way now to make sure that the state stays in sync with props is to do a deep comparison of new pros vs old props on each re-render? Or is there another approach I'm not thinking of that works better?
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Issue không nêu tên tệp nào trong repository, các bài kiểm thử hoặc các điểm vào. Trước tiên, hãy làm rõ liệu react.dev có nên viết tài liệu về các biểu mẫu được điều khiển với dữ liệu được tải bất đồng bộ hay không và xác định hướng dẫn dự kiến; hoàn thành nghĩa là có tài liệu trả lời cho kịch bản React và Redux đã mô tả.
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