observing of props in useEffect is inconsistent with component rendering

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

Maintainer thường phản hồi trong vòng 1 ngày

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

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
javascript, typescript
Lĩnh vực
frontend

Hướng nghiên cứu

Bắt đầu bằng cách chạy bản tái hiện Test và Root được cung cấp, rồi so sánh các props được đọc trong template với các props được đọc bên trong useEffect. Theo dõi cách props, signal, proxy và quá trình rendering tương tác với nhau; hoàn tất khi sự không nhất quán được giải quyết hoặc hành vi reactive-props được hỗ trợ được xác lập rõ ràng và được bao phủ bởi một regression test.

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

Mô tả

The following example prints 1 then 2 in the console, but never 3.
The template does render 1 then 2 then 3 however.

We expect all read of this.props.o.val to be reactive in the same way, but it is not the case.

I understand the canonical way to proceed here would be to pass the signal as props instead of its return value.
But the example is still valid, and the inconsistency of props in terms of effect and rendering is highly problematic.

Because of this, onWillUpdateProps cannot be simply converted to equivalent useEffect, because callers need to provide a signal.

import { Component, mount, xml, props, types, useEffect, proxy, signal } from "@odoo/owl";

class Test extends Component {
    static template = xml`<span t-out="this.props.o.val"/>`;
    props = props(["o"]);
    setup() {
        useEffect(() => console.log(this.props.o.val));
    }
}

class Root extends Component {
    static template = xml`<div>Hello Owl!<Test o="this.obj()"/></div>`;
    static components = { Test };

    obj = signal(proxy({ val: 1 }));

    setup() {
        setTimeout(() => this.obj().val = 2, 100);
        setTimeout(() => this.obj.set(proxy({ val: 3 })), 200);
    }
}

mount(Root, document.body, { templates: TEMPLATES, dev: true });

Side remark, but I don't understand why props.signal="..." is not the default if the given value is not a signal. It seems extremely confusing and error prone to allow non-reactive values as props, but with just a little bit of magic to still trigger re-rendering. This is even more strange as .alike already exists for case where we would want to disable the auto-wrapping behavior.

Ngôn ngữ chính
TypeScript
Star
1.6k
Fork
435
Merge trung bình
13 giờ 18 phút
Pull request đã merge (30 ngày)
5

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

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

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 odoo/owl

Tất cả issue của odoo/owl

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.