ข้ามไปยังเนื้อหา

Same vs Different: ตาราง Lookup

นี่คือ cheat sheet ของคุณ คอลัมน์ซ้ายแสดงสิ่งที่คุณรู้จาก React คอลัมน์ขวาแสดง Flutter equivalent รายการส่วนใหญ่ map แบบ one-to-one สิ่งที่แตกต่างจริงๆ จะมีหมายเหตุกำกับ

React conceptFlutter equivalentหมายเหตุ
Function componentStatelessWidgetClass ที่มี method build()
Class componentStatefulWidget + State<T>แยกเป็นสอง class
PropsConstructor parametersDart named params: MyWidget(name: 'Ava')
useStatesetState(() { ... })อยู่ใน State<T> subclass
useEffect (mount)initState()เรียกครั้งเดียวหลัง build แรก
useEffect (unmount)dispose()Clean up controllers, streams, timers
useEffect (deps)Manual didUpdateWidget / Riverpodไม่มี equivalent ตรงๆ
useContext / ContextInheritedWidget / Provider packageProvider คือ community standard
React.memoconst constructorDart const ป้องกัน rebuild
key propKey / super.keyวัตถุประสงค์เดียวกัน — tree reconciliation
JSXDart constructor calls<Text>Text('...')
CSS / StyleSheetWidget params + ThemeDataTextStyle, BoxDecoration ฯลฯ
FlexboxRow / ColumnmainAxisAlignmentjustify-content
display: noneConditional ด้วย if หรือ Visibilityif (show) MyWidget() ใน children list
react-router / Next.jsNavigator + routes / go_routerImperative หรือ declarative routing
npm / package.jsonpub / pubspec.yamlflutter pub add package_name
Fast RefreshHot Reload (r ใน terminal)เก็บ state ไว้; hot restart = full reload
.map() ใน JSX.map(...).toList() ใน childrenReturn List<Widget>
styled-componentsThemeData + widget paramsไม่มี runtime CSS — เป็น Dart compile-time ทั้งหมด
React.createContextInheritedWidget หรือ ChangeNotifierProvider wrap ทั้งสอง pattern
React
// React instinct — what you'd reach for
function ProfileCard({ name, isAdmin }) {
const [liked, setLiked] = useState(false);
return (
<div className="card" style={{ padding: 16, borderRadius: 12 }}>
<h3 style={{ margin: 0 }}>{name}</h3>
{isAdmin && <span className="badge">Admin</span>}
<button onClick={() => setLiked(l => !l)}>
{liked ? '❤️ Liked' : '🤍 Like'}
</button>
</div>
);
}
Flutter
// Flutter equivalent — layout is widgets
class ProfileCard extends StatefulWidget {
final String name;
final bool isAdmin;
const ProfileCard({super.key, required this.name, this.isAdmin = false});
@override
State<ProfileCard> createState() => _ProfileCardState();
}
class _ProfileCardState extends State<ProfileCard> {
bool _liked = false;
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(widget.name,
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
if (widget.isAdmin)
Container(
margin: const EdgeInsets.only(top: 4),
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
decoration: BoxDecoration(
color: Colors.blue[100],
borderRadius: BorderRadius.circular(4),
),
child: const Text('Admin',
style: TextStyle(color: Colors.blue, fontSize: 12)),
),
const SizedBox(height: 8),
GestureDetector(
onTap: () => setState(() => _liked = !_liked),
child: Text(
_liked ? '❤️ Liked' : '🤍 Like',
style: const TextStyle(fontSize: 16),
),
),
],
),
);
}
}
Flutter equivalent ของ npm / package.json คืออะไร?
ใน Flutter คุณทำให้เทียบเท่า CSS display: none เพื่อซ่อน widget แบบ conditional อย่างไร?
React's Fast Refresh map กับ Flutter feature ใด?
ทำไม Flutter ถึงแยก stateful component เป็น StatefulWidget + State<T>?