Skip to content

Computed values should keepAlive by default #4610

@bjb568

Description

@bjb568

Intended outcome: If I create a computed value and access it twice, it should only compute its value once (even if there is no reaction that depends on its value). This is the behavior of other state management libraries and of computed values in the signals standard proposal, so new users would likely expect this of MobX.

Actual outcome: Untracked computed values recompute the value each time the value is accessed.

How to reproduce the issue:

const computedValue = computed(() => console.log('computed'));
setInterval(() => computedValue.get(), 100);
// Should only log once

Passing { keepAlive: true } fixes the issue, but shouldn't that be the default behavior? With #1799 I don't see any downsides to having all computed values be keepAlive.

Note that this would likely be a semver major change.

Versions
6.13.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions