Skip to content

Recognize callback factory / currying #192

@elsassph

Description

@elsassph

Describe the need

It seems to be allowed to access props inside an onSomething function, e.g.

const onSomething = (): void => {
  const duration = props.duration;
  console.log(duration);
}

However using a factory/currying pattern causes reactivity issues:

// Warning: "This function should be passed to a tracked scope..."
const onSomething = (delay: number): void => () => {
  const duration = delay + props.duration;
  console.log(duration);
}

Even when attempting to name the inner function onSomething (not great anyway)

// Warning: "This function should be passed to a tracked scope..."
const createSomethingHandler = (delay: number): void => 
  function onSomething(): void {
    const duration = delay + props.duration;
    console.log(duration);
  }
}

Suggested Solution

Recognize the pattern :)

Possible Alternatives

Sometimes it is possible to pass all the props at the creation point (which would happen safely in an effect/JSX), but it isn't always practical.

Additional context

  • I would be willing to contribute a PR to implement this feature

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions