{"id":17351,"date":"2023-09-01T13:03:33","date_gmt":"2023-09-01T13:03:33","guid":{"rendered":"https:\/\/dialtous.com\/?p=17351"},"modified":"2023-09-01T13:03:33","modified_gmt":"2023-09-01T13:03:33","slug":"jsx-la-gi","status":"publish","type":"post","link":"https:\/\/dialtous.com\/jsx-la-gi\/","title":{"rendered":"Gi\u1edbi thi\u1ec7u JSX"},"content":{"rendered":"
Xem x\u00e9t \u0111o\u1ea1n khai b\u00e1o bi\u1ebfn d\u01b0\u1edbi \u0111\u00e2y:<\/p>\n
C\u00fa ph\u00e1p c\u1ee7a \u0111o\u1ea1n n\u00e0y kh\u00f4ng ph\u1ea3i l\u00e0 m\u1ed9t chu\u1ed7i k\u00ed t\u1ef1 v\u00e0 c\u0169ng kh\u00f4ng ph\u1ea3i l\u00e0 m\u1ed9t th\u1ebb HTML. \u0110\u00e2y \u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 JSX, m\u1ed9t c\u00fa ph\u00e1p m\u1edf r\u1ed9ng cho JavaScript. Ch\u00fang t\u00f4i khuy\u1ebfn kh\u00edch s\u1eed d\u1ee5ng JSX c\u00f9ng v\u1edbi React \u0111\u1ec3 mi\u00eau t\u1ea3 giao di\u1ec7n ng\u01b0\u1eddi d\u00f9ng (UI). JSX c\u00f3 th\u1ec3 tr\u00f4ng gi\u1ed1ng ng\u00f4n ng\u1eef template, nh\u01b0ng n\u00f3 k\u1ebft h\u1ee3p t\u1ea5t c\u1ea3 c\u00e1c t\u00ednh n\u0103ng c\u1ee7a JavaScript.<\/p>\n
JSX t\u1ea1o ra c\u00e1c ph\u1ea7n t\u1eed React. Ch\u00fang ta s\u1ebd t\u00ecm hi\u1ec3u c\u00e1ch ch\u00fang \u0111\u01b0\u1ee3c hi\u1ec3n th\u1ecb tr\u00ean DOM trong ph\u1ea7n ti\u1ebfp theo. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 nh\u1eefng ki\u1ebfn th\u1ee9c c\u01a1 b\u1ea3n v\u1ec1 JSX m\u00e0 b\u1ea1n c\u1ea7n \u0111\u1ec3 b\u1eaft \u0111\u1ea7u.<\/p>\n
React khuy\u1ebfn kh\u00edch ch\u00fang ta k\u1ebft h\u1ee3p logic render m\u1ed9t c\u00e1ch t\u1ef1 nhi\u00ean v\u1edbi c\u00e1c logic kh\u00e1c li\u00ean quan \u0111\u1ebfn giao di\u1ec7n ng\u01b0\u1eddi d\u00f9ng: x\u1eed l\u00fd s\u1ef1 ki\u1ec7n, thay \u0111\u1ed5i tr\u1ea1ng th\u00e1i theo th\u1eddi gian v\u00e0 chu\u1ea9n b\u1ecb d\u1eef li\u1ec7u \u0111\u1ec3 hi\u1ec3n th\u1ecb.<\/p>\n
Thay v\u00ec t\u00e1ch bi\u1ec7t c\u00e1c c\u00f4ng ngh\u1ec7 gi\u1ea3 t\u1ea1o b\u1eb1ng c\u00e1ch \u0111\u1ecbnh ngh\u0129a giao di\u1ec7n v\u00e0 logic trong c\u00e1c t\u1ec7p ri\u00eang bi\u1ec7t, React t\u00e1ch bi\u1ec7t ch\u00fang trong c\u00e1c \u0111\u01a1n v\u1ecb r\u1eddi r\u1ea1c g\u1ecdi l\u00e0 “components”. Ch\u00fang ta s\u1ebd th\u1ea3o lu\u1eadn v\u1ec1 components trong m\u1ed9t ph\u1ea7n kh\u00e1c, v\u00e0 n\u1ebfu b\u1ea1n v\u1eabn ch\u01b0a th\u1ea5y tho\u1ea3i m\u00e1i v\u1edbi vi\u1ec7c \u0111\u01b0a \u0111\u1ecbnh ngh\u0129a giao di\u1ec7n v\u00e0o trong JavaScript, bu\u1ed5i n\u00f3i chuy\u1ec7n n\u00e0y c\u00f3 th\u1ec3 thuy\u1ebft ph\u1ee5c b\u1ea1n.<\/p>\n
React kh\u00f4ng y\u00eau c\u1ea7u b\u1eaft bu\u1ed9c s\u1eed d\u1ee5ng JSX, nh\u01b0ng h\u1ea7u h\u1ebft m\u1ecdi ng\u01b0\u1eddi \u0111\u1ec1u th\u1ea5y n\u00f3 h\u1eefu \u00edch khi l\u00e0m vi\u1ec7c v\u1edbi giao di\u1ec7n ng\u01b0\u1eddi d\u00f9ng trong m\u00e3 JavaScript. JSX c\u0169ng cho ph\u00e9p React hi\u1ec3n th\u1ecb c\u00e1c th\u00f4ng b\u00e1o l\u1ed7i v\u00e0 c\u1ea3nh b\u00e1o h\u1eefu \u00edch h\u01a1n.<\/p>\n
V\u1eady b\u1eaft \u0111\u1ea7u ngay th\u00f4i!<\/p>\n
Trong v\u00ed d\u1ee5 d\u01b0\u1edbi \u0111\u00e2y, ch\u00fang ta khai b\u00e1o m\u1ed9t bi\u1ebfn c\u00f3 t\u00ean l\u00e0 “name” v\u00e0 s\u1eed d\u1ee5ng n\u00f3 b\u00ean trong JSX b\u1eb1ng c\u00e1ch \u0111\u1eb7t n\u00f3 trong c\u1eb7p d\u1ea5u ngo\u1eb7c nh\u1ecdn:<\/p>\n
B\u1ea1n c\u00f3 th\u1ec3 nh\u00fang b\u1ea5t k\u1ef3 bi\u1ec3u th\u1ee9c JavaScript h\u1ee3p l\u1ec7 n\u00e0o v\u00e0o trong JSX b\u1eb1ng c\u00e1ch \u0111\u1eb7t ch\u00fang trong c\u1eb7p d\u1ea5u ngo\u1eb7c nh\u1ecdn. V\u00ed d\u1ee5: 2 + 2, user.firstName ho\u1eb7c formatName(user) \u0111\u1ec1u l\u00e0 c\u00e1c bi\u1ec3u th\u1ee9c h\u1ee3p l\u1ec7 trong JavaScript.<\/p>\n
Trong v\u00ed d\u1ee5 d\u01b0\u1edbi \u0111\u00e2y, ch\u00fang ta nh\u00fang k\u1ebft qu\u1ea3 c\u1ee7a m\u1ed9t h\u00e0m JavaScript, formatName(user), v\u00e0o trong ph\u1ea7n t\u1eed <\/p>\n