I'm studying Flutter hooks. I have written a program that is supposed to exhibit the same behavior as Flutter and React hooks. However, I have noticed that the timing of cleanup execution in useEffect differs from React. I have read the official documentation for flutter_hooks, but I am still unclear about the reason behind this difference.
Is there anyone who can help me understand this better and provide some guidance?
Below is the program I wrote and the result of running it.
Flutter Program
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
void main() {
runApp(const MyHomePage());
}
class MyHomePage extends HookWidget {
const MyHomePage({super.key});
@override
Widget build(BuildContext context) {
print("build start");
final counter = useState<int>(0);
useEffect(() {
print("useEffect");
return () {
print("useEffect cleanup");
};
}, [counter.value]);
return Directionality(
textDirection: TextDirection.ltr,
child: Column(
children: [
Center(child: Text('${counter.value}')),
TextButton(
onPressed: () {
counter.value = counter.value + 1;
print('counter incremented');
},
child: const Text('+'))
],
));
}
}
React
index.jsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import { App } from './App.jsx'
ReactDOM.createRoot(
document.querySelector('#root')
).render(<App />)
App.jsx
import {useState, useEffect} from 'react';
export function App(props) {
console.log("build start");
const [count, setCount] = useState(0);
useEffect(() => {
console.log("useEffect");
return () => {
console.log("useEffect cleanup");
};
}, [count]);
const button1 = () => {
setCount((c) => c + 1);
console.log('counter incremented')
};
return (
<div className='App'>
<h1>{count}</h1>
<button onClick={button1}>+</button>
</div>
);
}
Result(Flutter program)
build start
useEffect
counter incremented
build start
useEffect
useEffect cleanup
counter incremented
build start
useEffect
useEffect cleanup
Result(React program)
build start
useEffect
counter incremented
build start
useEffect cleanup
useEffect
counter incremented
build start
useEffect cleanup
useEffect
Thanks :)