over_react_create_ref_usage

Severity: AnalysisErrorSeverity.WARNING

Maturity: stable

Since 1.0.0

View the Project on GitHub workiva/over_react

This diagnostic detects when createRef, instead of useRef, is used inside a function component, since usually it's a mistake.

createRef creates a new ref object each time, while useRef persists for the lifetime of the component.

BAD:

import 'package:over_react/over_react.dart';

part 'create_ref_usage.over_react.g.dart';

UiFactory<FooProps> Foo = uiFunction(
  (props) {
    final ref = createRef();

    return (Dom.div()..ref = ref)();
  },
  _$FooConfig, // ignore: undefined_identifier
);

mixin FooProps on UiProps {}

GOOD:

import 'package:over_react/over_react.dart';

part 'create_ref_usage.over_react.g.dart';

UiFactory<FooProps> Foo = uiFunction(
  (props) {
    final ref = useRef();

    return (Dom.div()..ref = ref)();
  },
  _$FooConfig, // ignore: undefined_identifier
);

mixin FooProps on UiProps {}