lundi 30 mars 2020

Angular @Input best practices

I have a question about the use of @Input in a component. My problem concern the use of non-primitive object. So the variable is pass by reference as the following code :

@Input() public data: any;

In a lot of project I can see some people changed directly the data as following into the child.

this.data.name = 'something';

For me it's a problem because we change also the value into the parent, and to do that we have to use an @Output. By definition an @Input is only for one way, parent to child.

For me it's an anti-pattern, is it right ?

Each time my solution is to clone the @Input into a child attribute and send it back edited by @Output to the parent. Or only the part of the object edited because for me only the parent have the responsability to edit the entire object.

Do you have a basic solution or a pattern ? Maybe the problem is because the app isn't well design and have to be more divided. Because I think it's a common problem on each app but I didn't find something in internet.

Aucun commentaire:

Enregistrer un commentaire