Skip to content

Clarify Interactivity API evaluate function and directives to not allow action usage for what should be state #69269

@felixarntz

Description

@felixarntz

This came up as part of working on #68097 in conversation with @luisherranz, specifically in the comments starting with #68097 (review).

Problems to address:

  • Some developers today rely on store functions such as actions to determine the value for attributes. However, values should always use context or derived state, so this is an anti-pattern that should be more strongly discouraged or even made impossible.
  • The evaluate() function today invokes functions, whereas in relation to the above it should only look up the function and return it, just like it does for regular values.

As part of the PR #68097, the behavior was already moved closer towards the right direction, but it's definitely in a temporary state. Here's a list of a few more low-level technical issues that should be ironed out for a proper solution:

  • Currently, every caller of evaluate() needs to check whether the return value is a function and, if so, invoke it. This should be made more ergonomic.
  • Currently, it is possible to provide reference to a function in combination with a negation operator. This should no longer be allowed because functions should only be used for actions like for data-wp-on, callbacks like for data-wp-watch etc - i.e. functions should never return a value, and thus a negation operator for them is irrelevant.

One part that's already prepared for a step in the right direction is that providing a reference to a function in combination with a negation operator will trigger a console warning going forward, since this should never be used, as functions should never be used to return values in the first place. For now it still works due to backward compatibility, but is now deprecated and can eventually be removed.

Let's discuss in this issue what would be a good strategy to get to the end goal. We also need to consider potential in-between steps, such as first deprecating something that is discouraged, launching that in a release, and only later making it impossible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Developer ExperienceIdeas about improving block and theme developer experience[Feature] Interactivity APIAPI to add frontend interactivity to blocks.[Type] EnhancementA suggestion for improvement.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions