Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State change in useEffect doesn't trigger rebuild #454

Closed
zaikir opened this issue Jan 5, 2025 · 1 comment
Closed

State change in useEffect doesn't trigger rebuild #454

zaikir opened this issue Jan 5, 2025 · 1 comment
Assignees
Labels
bug Something isn't working needs triage

Comments

@zaikir
Copy link

zaikir commented Jan 5, 2025

Describe the bug
First of all, great library! But I faced strange issue and can't understand -- or this is a bug, or I just missed something. The problem is related to how useEffect works -- it seems like it doesn't trigger rebuild when state inside it changes.
In my example, I do input.value += '1' by pressing a button and I see 5 printed lines. But technically it should be 6, right?
Because in useEffect we change the state and it should retrigger build (otherwise useMemoized not updating)

Did I miss something? Because in React.js this example would work

To Reproduce

final input = useState('');
final confirm = useState('');

print('input: $input.value, confirm: $confirm.value');

final subtitleText = useMemoized(() {
  return confirm.value.isEmpty ? 'Create New Passocde' : 'Confirm Passcode';
}, [confirm.value]);

useEffect(() {
  if (input.value.length == 4) {
    confirm.value = input.value;
  }

  return null;
}, [input.value]);

Expected behavior
I see 5 lines of printed text but it should be 6:

flutter: input: ValueNotifier<String>#87874().value, confirm: ValueNotifier<String>#8edb3().value
flutter: input: ValueNotifier<String>#87874(1).value, confirm: ValueNotifier<String>#8edb3().value
flutter: input: ValueNotifier<String>#87874(11).value, confirm: ValueNotifier<String>#8edb3().value
flutter: input: ValueNotifier<String>#87874(111).value, confirm: ValueNotifier<String>#8edb3().value
flutter: input: ValueNotifier<String>#87874(1111).value, confirm: ValueNotifier<String>#8edb3().value
@zaikir zaikir added bug Something isn't working needs triage labels Jan 5, 2025
@rrousselGit
Copy link
Owner

That's expected. The callback executes synchronously.

That's like doing:

build(context) {
  setState(() {});
}

This won't rebuild the widget

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

2 participants