reactjs / reactjs/react.dev

React docs on "Building Your Own Hooks"

Đang mở
#2,939 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ả

Please provide guidance if returning elements (or ()=>elements) from custom hook is a good practice or not.

We have a PageLayout component like the following in our own app:

    <PageLayout
      topBarButtons={..}
      onRender={..}
    />

We want to encapsulate topBarButtons and onRender logic within a custom hook, so that the hook is reusable and shippable without PageLayout. (topBarButtons and onRender will share some state, and useOurHook returns [topBarButtons, onRenderFunc]).

This works fine, if not perfect, for me. But since there are so few custom hook example that returns UI elements (or function that returns UI elements like onRender), the team have doubt about this pattern.

I know the doc has the following statement, but I think this is a very common scenario that deserves a sentence or two in the React docs.

"useYourImagination()
Custom Hooks offer the flexibility of sharing logic that wasn’t possible in React components before. You can write custom Hooks that cover a wide range of use cases like form handling, animation, declarative subscriptions, timers, and probably many more we haven’t considered."

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 trang tài liệu "Building Your Own Hooks" và xem lại các hướng dẫn cũng như ví dụ hiện có về các Hooks tùy chỉnh. Thêm một giải thích ngắn gọn về việc trả về các phần tử hoặc các hàm trả về các phần tử có phải là một pattern phù hợp hay không, bao gồm cả kịch bản trạng thái dùng chung được mô tả ở đây; trang này phải trả lời câu hỏi của nhóm mà không yêu cầu ngữ cảnh dành riêng cho ứng dụng của issue này.

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ó
2/5
Thời gian dự kiến
1-3 giờ
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
50/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.