12

I'm kinda new to ngrx and I've encountered this little problem I haven't figured out how to solve yet.

Basically I have a ListComponent that renders an array of ListItemComponents from a ngrx store.

@Component({ 
  ...
  template: `
    <list-item *ngFor="let item of item$ | async" [data]="item">
    </list-item>
  `
})
export class ListComponent implements OnInit {
  item$: Observable<ListItem>;
  constructor(private store: Store<ListState>) {}

  ngOnInit() {
    this.item$ = this.store.select(selectListItems);
  }
}

Now, inside ListItemComponent, under some specific circumstances I want to render another ListComponent where I can add items to, however this doesn't work as I get a Maximum call stack size exceeded error.

My guess is, and correct me if I'm wrong, since the nested ListComponent is accessing the same slice of state as the root ListComponent, its just trying to nest and render the same list over and over and over again into infinity.

So here's the question, how should I compose my selectors in order to provide the correct entry point in the state tree to each nested ListComponent?


Edit

Here's a working stackblitz project, now the logic in my app to render a List inside a ListItem is different but the problem is the same.

6
  • 1
    u just need to pass selectListItems as Input in ListComponent
    – Dhyey
    Jan 6, 2018 at 10:34
  • care to elaborate how that would solve my issue? I mean, in order to pass the function as input, should I that make my ListStepComponent store aware as well?
    – Osman Cea
    Jan 6, 2018 at 10:45
  • 3
    can u pls post the code for selectListItems and ListStepComponent ?
    – Dhyey
    Jan 6, 2018 at 10:49
  • Yes, give me some time to reproduce the example in stackblitz
    – Osman Cea
    Jan 6, 2018 at 17:07
  • 1
    1 control data with app component - smart component , 2 assign items as an @Input, 3. change state to a tree structure like items:[{name:'item1',items:[]},{name:'item2',items:[]}]
    – Fan Cheung
    Jan 8, 2018 at 12:41

1 Answer 1

1
+50

I think you're right:

My guess is, and correct me if I'm wrong, since the nested ListComponent is accessing the same slice of state as the root ListComponent, its just trying to nest and render the same list over and over and over again into infinity.

Try updating ListComponent to accept a component input variable of ListItemComponent. See working example https://stackblitz.com/edit/angular-ngrx-recursive-components-7mzncj

In that example, the 5th ListItemComponent also renders a ListComponent. However, when clicking the "add element to list" button on that 5th ListItemComponent, the code is still adding the new elements to the parent list. I'm guessing you'll want to update your logic for how that is handled. Not sure what you're trying to accomplish.

The core problem is that the list is generating itself. So if a list generates another list, you run into the recursive problem. By using component input variables, you can stop the list from generating itself and eliminate the recursive problem.

Update:

I came to this answer on my own, but I just noticed that @FanCheung, in the comments, previously proposed what is basically this solution.

6
  • Yeah, after reading @FanCheung comment I basically came to the same solution as you're providing. The problem now is adding the new generated list item in the corresponding child list. I think I should update the reducer to iterate through the whole tree, find the correct node from where the action was dispatched, (perhaps with some unique id?) and return the state... the problem i see with that approach is, since the selector is selecting the root list, whenever I add a nested step, it's gonna update the whole state tree, whilst I'd prefer the selector to only affect the corresponding branch
    – Osman Cea
    Jan 9, 2018 at 14:40
  • @OsmanCea actually I don't think you need to worry about updating the whole state tree (at least performance wise). If you push out a new state tree, but only a bit of it has changed, I think angular is smart enough to only update those pieces of the DOM that have actually changed. Ya, there will be a bit of overhead as angular looks at the diffs, but I think it will be negligible (unless your lists are REALLY large, I suppose).
    – John
    Jan 9, 2018 at 15:11
  • As an example, if you subscribe to a list in the store and then something is appended to the list, your subscription will receive a whole new copy of the list. Angular is smart enough to only actually add the new item on the end though. If your list items are each rendered by their own component, you could test this by console.log during ngOnChanges. When you append something to the list you'll notice the existing list elements won't run their OnChanges. The only diff is adding a new element. Angular is very smart :)
    – John
    Jan 9, 2018 at 15:14
  • since no one else cared to give me an answer, you get the bounty ;)
    – Osman Cea
    Jan 15, 2018 at 15:26
  • 1
    I did solved my problem though (it implied generating some unique id's for every list component, getting a path of a given id, and doing some deep state update immutable stuff but hey, it was worth it)
    – Osman Cea
    Jan 15, 2018 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.