Angular – react to changes in rxResource()

How one can react to changes / perform side-effects when rxResource receives .value() instead of using effect()?

Current approach in component:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> constructor() {
this.service.setItemId(1); //setting signal
effect(() => // work with data from async post request
}
</code>
<code> constructor() { this.service.setItemId(1); //setting signal effect(() => // work with data from async post request } </code>
  constructor() {
    this.service.setItemId(1); //setting signal
    effect(() => // work with data from async post request 
  } 

in service:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> resource$ = rxResource({
request: () => this.itemID$(), // signal
loader: ({ request: id }) => {
return this.http.post('http://');
}
});
</code>
<code> resource$ = rxResource({ request: () => this.itemID$(), // signal loader: ({ request: id }) => { return this.http.post('http://'); } }); </code>
 resource$ = rxResource({
    request: () => this.itemID$(), // signal 
    loader: ({ request: id }) => {
      return this.http.post('http://');
    }
  }); 

3

UPDATE:

As per the requirements, it looks like you want to react to the creation of a new item in the database, but fetching the elements from the database reactively.

In the below code, we trigger the post on change of itemID$ value, I have used a modal here, this updates the database (PUT) and then we need to read the data to see if it is updated.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>updatePost: ResourceRef<any> = rxResource({
request: () => this.itemID$(), // signal
loader: ({ request: id }) => {
console.log(id);
return this.http.put(`http://localhost:3000/posts/1`, {
id: 1,
title: `from resource - ${id}`,
views: id * 200,
userId: 1,
});
},
});
</code>
<code>updatePost: ResourceRef<any> = rxResource({ request: () => this.itemID$(), // signal loader: ({ request: id }) => { console.log(id); return this.http.put(`http://localhost:3000/posts/1`, { id: 1, title: `from resource - ${id}`, views: id * 200, userId: 1, }); }, }); </code>
updatePost: ResourceRef<any> = rxResource({
  request: () => this.itemID$(), // signal
  loader: ({ request: id }) => {
    console.log(id);
    return this.http.put(`http://localhost:3000/posts/1`, {
      id: 1,
      title: `from resource - ${id}`,
      views: id * 200,
      userId: 1,
    });
  },
});

All you need to do is pass the this.updatePost.value() signal execution as a input to the request method, this will trigger the GET api call (getPosts resource), on each post request from the rxResource (updatePost)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>getPosts: ResourceRef<any> = rxResource({
request: () => this.updatePost.value(), // signal
loader: (params: ResourceLoaderParams<any>) => {
console.log(params.request);
return this.http.get(`http://localhost:3000/posts`);
},
});
</code>
<code>getPosts: ResourceRef<any> = rxResource({ request: () => this.updatePost.value(), // signal loader: (params: ResourceLoaderParams<any>) => { console.log(params.request); return this.http.get(`http://localhost:3000/posts`); }, }); </code>
getPosts: ResourceRef<any> = rxResource({
  request: () => this.updatePost.value(), // signal
  loader: (params: ResourceLoaderParams<any>) => {
    console.log(params.request);
    return this.http.get(`http://localhost:3000/posts`);
  },
});

In the below stackblitz, we update a row in the database, then we get the rows, we can see that the row has been updated.

  1. Open new terminal in stackblitz and run npx json-server db.json.

  2. Then restart the angular server and click on localhost:4200 in the terminal to view the app in the preview and try out the feature.

Stackblitz Demo



If you want to derive state from the original signal, then you can use computed, linkedSignal to do this.

But since you are doing DOM manipulations based on an API call, the best option is to use effect to do this, else you can try to achieve the same result without any DOM manipulations.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>constructor() {
effect(() => {
// .. DOM manuipulations.
})
}
</code>
<code>constructor() { effect(() => { // .. DOM manuipulations. }) } </code>
constructor() {
  effect(() => {
    // .. DOM manuipulations.
  })
}

Ideally you should be able to achieve the same result without DOM manipulations based on signal reactivity, but those details need to be provided for a clear answer.

8

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật