#Binding functions in template - avoiding change detection

1 messages · Page 1 of 1 (latest)

zealous rivet
#

I've bound a function in a template (basically passing a function to the child component to run) and it seems that change detection picks them up as new values every time change detection runs. Is there any way to avoid this?

This is what the input looks like on the child:
@Input() passedFunction: (entity: T) => void;

arctic thorn
#

how are you passing the function in

zealous rivet
#
[passedFunction]="theFunction()"
</app-child-component>

.ts file

return () => doSomething()
}
rancid cape
#

So you're creating a new function at each change detection. Don't.

arctic thorn
#

You're creating a new function on every change detection run, that's why it picks it up as a new value

zealous rivet
#

right, that makes sense... any way i can retain this. binding without doing this?

arctic thorn
#
theFunction = () => doSomething()
#
[passedFunction]="theFunction"
zealous rivet
#

Cheers!

hoary wren
#

Passing functions through @Input would never be something I recommend. I think that is a not great design and an anti-pattern

zealous rivet
#

I have considered it briefly since I have't seen it done usually. But I'm not sure what alternative approach I would take. I have a 'mapping' function that gets passed in a table component otherwise the component is pretty much the same for a bunch of different data sets

grizzled plume
#

Would still avoid if possible, but it does happen more often with tables.

zealous rivet
#

I also need the 'raw type' (unmapped) in it since it gets passed to a detail component (legacy code I'm planning to update)

rain coral
#

if you have shared small function, just export it