ManuelDeLeon / viewmodel-react

Create your React components with view models.
MIT License
24 stars 3 forks source link

Suggestion: cached/computed properties #48

Open wildhart opened 5 years ago

wildhart commented 5 years ago

After playing a bit with vue.js I really like its computed properties and would like to see something similar in ViewModel.

Because the full render function of a component re-runs on each update, any methods used as part of the render function are constantly re-running even if none of their reactive dependancies have changed. This can be expensive. Vue's computed properties solves this problem.

Since using vue.js I've been achieving the same result in ViewModel by using autoruns like this:

Component({
    name: '',
    computedPropertyA: 0,
    computedPropertyB: 0,

    autorun: [
        function() { // computedPropertyA
            this.computedPropertyA(expensiveFunction());
        },
        function() { // computedPropertyB
            this.computedPropertyB(anotherExpensiveFunction(this.name()));
        },
    ],
    ....

This means that the expensiveFunctions are only re-run when their reactive dependencies change.

I'd love to see something like this built into ViewModel somehow!