Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Add a reset history method to useStateWithHistory

Đang mở
#2,519 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.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
48/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
react, typescript
Lĩnh vực
frontend

Hướng nghiên cứu

Start by locating the TypeScript implementation and tests for useStateWithHistory, then compare its API with useUndoable's resetInitialState. Add a reset method that accepts the new state, makes it the sole history entry, and leaves rewinding unable to reach the previous initial value; update or add tests for the reset behavior.

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

Mô tả

Is your feature request related to a problem? Please describe.
useUndoable has a great summary of why resetting state on useStateWithHistory is useful. I set my state to the response of a network request and want to prevent the user being able to rewind history to the initial value set before the request had finished.

Describe the solution you'd like
Add a method to useStateWithHistory that follows the API of useUndoable's resetInitialState, where it takes in a param to set the state to and clears the history.

Describe alternatives you've considered
There are two alternatives I've been using:

  1. using useUndoable instead of this library (but it doesn't work for deeply nested objects and is an additional dependency)
  2. Manually extending the useStateWithHistory myself to include this method. The method is really easy to add:
  const resetInitialState = useCallback(
    (newInitialState: IHookStateInitAction<S>) => {
      innerSetState(newInitialState as S);
      history.current = [newInitialState as I];
      historyPosition.current = 0;
    },
    [],
  );

This seems to have been requested already in https://github.com/streamich/react-use/issues/1386 but I'm re-raising here because:
I believe the interface of useUndoable is relevant, I'm providing a proposed solution, and that issue has been open for over 2 years.

If approved i'd even be happy to raise a PR myself.

Thanks!

Ngôn ngữ chính
TypeScript
Star
44k
Fork
3.3k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

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.

Issue khác của streamich/react-use

Tất cả issue của streamich/react-use

Issue tương tự

Thêm issue về TypeScript

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.