where I need to ask for object information. In parent or in child?

41 Views Asked by At

I have a table with a list of objects, and I provide the user with an option to edit these objects. I don't know if I need to use the API in the parent (object list), in the child (object) or in both. Can I replace 2 calls to the API with 1 observer (rxjs) in the parent and one @Input in the child? Is this a good idea?

ParentList(){
  listObjects$: Observable<Object[]>;

   this.listObjects$.pipe(take(1)).subscribe(list => {
     //Assign listObjects
   });
}
ChildForm(){
  @Input() object$!: Observable<Object>;

}

I actually tried calling the API service both times.

1

There are 1 best solutions below

0
DEV_404_NF On

I would suggest to call the API either from child component or from parent component. I don't see a reason to call it from both.

Regarding whether to call it from the parent or child component, it depends:

  • If your API accepts one object to edit per request, then better to make the call from the child component.
  • However, if your API accepts multiple objects to edit per request, it's better to make the call from the parent component. You can pass every edited object from child to parent then call the API.