diff --git a/src/content/learn/passing-props-to-a-component.md b/src/content/learn/passing-props-to-a-component.md index da5fc5ef..3ee3cdd7 100644 --- a/src/content/learn/passing-props-to-a-component.md +++ b/src/content/learn/passing-props-to-a-component.md @@ -1,26 +1,26 @@ --- -title: Passing Props to a Component +title: ການສົ່ງ Props ໄປຫາ Component --- -React components use *props* to communicate with each other. Every parent component can pass some information to its child components by giving them props. Props might remind you of HTML attributes, but you can pass any JavaScript value through them, including objects, arrays, and functions. +Component React ໃຊ້ *props* ເພື່ອສື່ສານລະຫວ່າງກັນ ແລະ ກັນ. ທຸກໆ parent component ສາມາດສົ່ງບາງຂໍ້ມູນໄປຫາ child component ມັນເອງໂດຍການໃຫ້ props. Props ອາດເຕືອນທ່ານກ່ຽວກັບ attribute HTML, ແຕ່ທ່ານສາມາດສົ່ງຄ່າຂອງ JavaScript ໃດກໍໄດ້ຜ່ານມັນລວມເຖິງ objects, arrays, ແລະ ຟັງຊັ່ນ. -* How to pass props to a component -* How to read props from a component -* How to specify default values for props -* How to pass some JSX to a component -* How props change over time +* ວິທີສົ່ງ props ໄປຫາ component +* ວິທີອ່ານ props ຈາກ component +* ວິທີກຳນົດຄ່າເລີ່ມຕົ້ນສຳລັບ props +* ວິທີສົ່ງບາງ JSX ໄປຫາ component +* ວິທີທີ່ props ປ່ຽນແປງຕະຫຼອດເວລາ -## Familiar props {/*familiar-props*/} +## ຄຸ້ນເຄີຍກັບ props {/*familiar-props*/} -Props are the information that you pass to a JSX tag. For example, `className`, `src`, `alt`, `width`, and `height` are some of the props you can pass to an ``: +Props ແມ່ນຂໍ້ມູນທີ່ທ່ານສົ່ງຜ່ານແທັກ JSX. ຕົວຢ່າງ, `className`, `src`, `alt`, `width`, ແລະ `height` ແມ່ນບາງ props ທີ່ທ່ານສາມາດສົ່ງຜ່ານ ``: @@ -51,11 +51,11 @@ body { min-height: 120px; } -The props you can pass to an `` tag are predefined (ReactDOM conforms to [the HTML standard](https://www.w3.org/TR/html52/semantics-embedded-content.html#the-img-element)). But you can pass any props to *your own* components, such as ``, to customize them. Here's how! +Props ສາມາດສົ່ງຜ່ານແທັກ `` ນັ້ນຖືກກຳນົດແລ້ວ (ReactDOM conforms to [the HTML standard](https://www.w3.org/TR/html52/semantics-embedded-content.html#the-img-element)). ແຕ່ທ່ານສາມາດສົ່ງ props ໃດກໍ່ໄດ້ໄປຫາ component *ຂອງທ່ານເອງ*, ເຊັ່ນ ``, ເພື່ອປັບແຕ່ງ. ນີ້ແມ່ນວິທີ! -## Passing props to a component {/*passing-props-to-a-component*/} +## ການສົ່ງ props ໄປຫາ component {/*passing-props-to-a-component*/} -In this code, the `Profile` component isn't passing any props to its child component, `Avatar`: +ໃນ code ນີ້, Component `Profile` ບໍ່ສາມາດສົ່ງ props ໃດກໍໄດ້ໄປຫາ child component, `Avatar`: ```js export default function Profile() { @@ -65,11 +65,11 @@ export default function Profile() { } ``` -You can give `Avatar` some props in two steps. +ທ່ານສາມາດໃຫ້ props `Avatar` ໃນສອງຂັ້ນຕອນ. -### Step 1: Pass props to the child component {/*step-1-pass-props-to-the-child-component*/} +### ຂັ້ນຕອນທີ 1: ສົ່ງ props ໄປຫາ child component {/*step-1-pass-props-to-the-child-component*/} -First, pass some props to `Avatar`. For example, let's pass two props: `person` (an object), and `size` (a number): +ທຳອິດ, ສົ່ງ props ໄປຫາ `Avatar`. ຕົວຢ່າງ, ສົ່ງ 2 props: `person` (ເປັນ object), ແລະ `size` (ເປັນ number): ```js export default function Profile() { @@ -84,15 +84,15 @@ export default function Profile() { -If double curly braces after `person=` confuse you, recall [they're merely an object](/learn/javascript-in-jsx-with-curly-braces#using-double-curlies-css-and-other-objects-in-jsx) inside the JSX curlies. +ຖ້າສອງວົງປີກກາຢູ່ຫຼັງ `person=` ເຮັດໃຫ້ທ່ານສັບສົນ, ໃຫ້ຈື່ໄວ້ວ່າ [ມັນເປັນ object](/learn/javascript-in-jsx-with-curly-braces#using-double-curlies-css-and-other-objects-in-jsx) ຢູ່ພາຍໃນວົງປີກກາ JSX. -Now you can read these props inside the `Avatar` component. +ທ່ານສາມາດອ່ານ props ນີ້ພາຍໃນ component `Avatar` -### Step 2: Read props inside the child component {/*step-2-read-props-inside-the-child-component*/} +### ຂັ້ນຕອນທີ 2: ການອ່ານ props ພາຍໃນ child component {/*step-2-read-props-inside-the-child-component*/} -You can read these props by listing their names `person, size` separated by the commas inside `({` and `})` directly after `function Avatar`. This lets you use them inside the `Avatar` code, like you would with a variable. +ທ່ານສາມາດອ່ານ props ໂດຍລະບຸຊື່ `person, size` ແຍກກັນໂດຍໃສ່ເຄື່ອງໝາຍຈຸດພາຍໃນ `({` ແລະ `})` ໂດຍກົງຫຼັງຈາກ `function Avatar`. ນີ້ເຮັດໃຫ້ທ່ານສາມາດນຳໃຊ້ພາຍໃນ code `Avatar`, ຄືກັບທີທ່ານໃຊ້ກັບຕົວແປ. ```js function Avatar({ person, size }) { @@ -100,9 +100,9 @@ function Avatar({ person, size }) { } ``` -Add some logic to `Avatar` that uses the `person` and `size` props for rendering, and you're done. +ເພີ່ມ logic ໃສ່ `Avatar` ທີ່ໃຊ້ props `person` ແລະ `size` ສຳລັບການສະແດງຜົນເທົ່ານີ້ກໍສຳເລັດ. -Now you can configure `Avatar` to render in many different ways with different props. Try tweaking the values! +ຕອນນີ້ທ່ານສາມາດຕັ້ງຄ່າ `Avatar` ເພື່ອສະແດງໄດ້ຫຼາຍວິທີດ້ວຍຫຼາຍ prop ທີ່ແຕກຕ່າງກັນ. ລອງປັບຫຼິ້ນເບິ່ງ! @@ -168,9 +168,9 @@ body { min-height: 120px; } -Props let you think about parent and child components independently. For example, you can change the `person` or the `size` props inside `Profile` without having to think about how `Avatar` uses them. Similarly, you can change how the `Avatar` uses these props, without looking at the `Profile`. +Props ເຮັດໃຫ້ທ່ານຄິດກ່ຽວກັບ parent ແລະ child component ຢ່າງອິດສະຫຼະ. ຕົວຢ່າງ, ທ່ານສາມາດປ່ຽນ prop `person` ຫຼື `size` ພາຍໃນ `Profile` ໂດຍບໍ່ຈຳເປັນຕ້ອງຄິດວ່າ `Avatar` ໃຊ້ສິ່ງນີ້ແນວໃດ. ຄ້າຍຄືກັນ, ທ່ານສາມາດປ່ຽນວິທີທີ່ `Avatar` ໃຊ້ props, ໂດຍບໍ່ຕ້ອງເບິ່ງທີ່ `Profile`. -You can think of props like "knobs" that you can adjust. They serve the same role as arguments serve for functions—in fact, props _are_ the only argument to your component! React component functions accept a single argument, a `props` object: +ທ່ານສາມາດຄິດວ່າ props ຄືກັບລູກບິດທີ່ທ່ານສາມາດປັບໄດ້. ມັນເຮັດໜ້າທີ່ເປັນ argument ທີ່ເຮັດວຽກສຳລັບຟັງຊັ່ນ-ຄວາມຈິງ, props _ເປັນ_ argument ດຽວສຳລັບ component ຂອງທ່ານ! ຟັງຊັ່ນ component React ຮັບ argument ດຽວ, `props` object: ```js function Avatar(props) { @@ -180,11 +180,11 @@ function Avatar(props) { } ``` -Usually you don't need the whole `props` object itself, so you destructure it into individual props. +ປົກະຕິແລ້ວທ່ານບໍ່ຈຳເປັນຕ້ອງໃຊ້ `props` object ທັງໝົດ, ສະນັ້ນ ທ່ານສາມາດແຕກມັນອອກເປັນ props ຍ່ອຍແຕ່ລະອັນ. -**Don't miss the pair of `{` and `}` curlies** inside of `(` and `)` when declaring props: +**ຢ່າລືມວົງປີກກາ `{` ແລະ `}`** ພາຍໃນຂອງ `(` ແລະ `)` ເມື່ອປະກາດ props: ```js function Avatar({ person, size }) { @@ -192,7 +192,7 @@ function Avatar({ person, size }) { } ``` -This syntax is called ["destructuring"](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Unpacking_fields_from_objects_passed_as_a_function_parameter) and is equivalent to reading properties from a function parameter: +Syntax ເອີ້ນວ່າ ["destructuring"](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Unpacking_fields_from_objects_passed_as_a_function_parameter) ແລະ ເທົ່າກັນກັບການອ່ານຄ່າ propertie ຈາກ function parameter: ```js function Avatar(props) { @@ -204,9 +204,9 @@ function Avatar(props) { -## Specifying a default value for a prop {/*specifying-a-default-value-for-a-prop*/} +## ການລະບຸຄ່າເລີ່ມຕົ້ນສຳລັບ prop {/*specifying-a-default-value-for-a-prop*/} -If you want to give a prop a default value to fall back on when no value is specified, you can do it with the destructuring by putting `=` and the default value right after the parameter: +ຫາກທ່ານຕ້ອງການໃຫ້ຄ່າ prop ເລີ່ມຕົ້ນເພື່ອໃຊ້ແທນເມື່ອບໍ່ໄດ້ລະບຸຄ່າໄວ້, ທ່ານສາມາດເຮັດໄດ້ໂດຍການ destructuring ໂດຍໃສ່ `=` ແລະ ຄ່າເລີ່ມຕົ້ນໄວ້ຫຼັງ parameter: ```js function Avatar({ person, size = 100 }) { @@ -214,13 +214,13 @@ function Avatar({ person, size = 100 }) { } ``` -Now, if `` is rendered with no `size` prop, the `size` will be set to `100`. +ຕອນນີ້, ຖ້າ `` ສະແດງໂດຍບໍ່ມີ prop `size`, `size` ຈະຖືກຕັ້ງຄ່າເປັນ `100`. -The default value is only used if the `size` prop is missing or if you pass `size={undefined}`. But if you pass `size={null}` or `size={0}`, the default value will **not** be used. +ຄ່າເລີ່ມຕົ້ນຈະຖືກໃຊ້ກໍຕໍ່ເມືອບໍ່ມີ prop `size` ຫຼື ຖ້າທ່ານສົ່ງ `size={undefined}`. ແຕ່ຖ້າທ່ານສົ່ງ `size={null}` ຫຼື `size={0}`, ຄ່າເລີ່ມຕົ້ນຈະ **ບໍ່ຖືກ** ໃຊ້. -## Forwarding props with the JSX spread syntax {/*forwarding-props-with-the-jsx-spread-syntax*/} +## ການສົ່ງຕໍ່ props ດ້ວຍ spread syntax JSX {/*forwarding-props-with-the-jsx-spread-syntax*/} -Sometimes, passing props gets very repetitive: +ບາງເທື່ອ, ການສົ່ງ props ແມ່ນຊໍ້າກັນຫຼາຍ: ```js function Profile({ person, size, isSepia, thickBorder }) { @@ -237,7 +237,7 @@ function Profile({ person, size, isSepia, thickBorder }) { } ``` -There's nothing wrong with repetitive code—it can be more legible. But at times you may value conciseness. Some components forward all of their props to their children, like how this `Profile` does with `Avatar`. Because they don't use any of their props directly, it can make sense to use a more concise "spread" syntax: +ມັນບໍ່ມີຫຍັງຜິດຖ້າ code ຊໍ້າ-ມັນສາມາດອ່ານໄດ້ຊັດເຈນຂຶ້ນ. ແຕ່ບາງຄັ້ງທ່ານອາດໃຫ້ຄວາມສຳຄັນກັບຄວາມກະທັດຮັດ. ບາງ component ສົ່ງຜ່ານ props ທັງໝົດໄປຫາ children ຂອງມັນທັງໝົດ, ເຊັ່ນດຽວກັບວິທີ `Profile` ນີ້ເຮັດກັບ `Avatar`. ເພາະວ່າພວກມັນບໍ່ໃຊ້ prop ໂດຍກົງ, ຈຶ່ງເໝາະສົມທີ່ຈະໃຊ້ syntax "spread" ທີກະທັດຮັດກວ່ານີ້: ```js function Profile(props) { @@ -249,13 +249,13 @@ function Profile(props) { } ``` -This forwards all of `Profile`'s props to the `Avatar` without listing each of their names. +ນີ້ສົ່ງຕໍ່ prop ທັງໝົດຂອງ `Profile` ໄປຫາ `Avatar` ໂດຍບໍ່ລະບຸຊື່ແຕ່ລະລາຍການ. -**Use spread syntax with restraint.** If you're using it in every other component, something is wrong. Often, it indicates that you should split your components and pass children as JSX. More on that next! +**ໃຊ້ spread syntax ດ້ວຍຄວາມລະມັດລະວັງ.** ຖ້າທ່ານໃຊ້ມັນທຸກໆ component ອື່ນ, ສະແດງວ່າມີບາງຢ່າງຜິດປົກະຕິ. ສ່ວນຫຼາຍ, ມັນບົ່ງບອກວ່າທ່ານຄວນຈະແຍກ component ແລະ ສົ່ງ children ເປັນ JSX. ເພີ່ມເຕີມຕໍ່ໄປ! -## Passing JSX as children {/*passing-jsx-as-children*/} +## ການສົ່ງ JSX ເປັນ children {/*passing-jsx-as-children*/} -It is common to nest built-in browser tags: +ມັນເປັນເລື່ອງປົກະຕິທີ່ຈະຊ້ອນ built-in ແທັກບາວເຊີ: ```js
@@ -263,7 +263,7 @@ It is common to nest built-in browser tags:
``` -Sometimes you'll want to nest your own components the same way: +ບາງເທື່ອທ່ານຕ້ອງການຊ້ອນ component ຂອງທ່ານດ້ວຍຮູບແບບດຽວກັນ: ```js @@ -271,7 +271,7 @@ Sometimes you'll want to nest your own components the same way: ``` -When you nest content inside a JSX tag, the parent component will receive that content in a prop called `children`. For example, the `Card` component below will receive a `children` prop set to `` and render it in a wrapper div: +ເມື່ອທ່ານຊ້ອນເນື້ອຫາພາຍໃນແທັກ JSX, parent component ຈະຮັບເນື້ອຫາໃນ prop ເອີ້ນວ່າ `children`. ຕົວຢ່າງ, component `Card` ດ້ານລຸ່ມຈະຮັບ prop `children` ຕັ້ງຄ່າໃຫ້ `` ແລະ ສະແດງຜົນທາງໃນ div ທີ່ຄອບ: @@ -347,17 +347,18 @@ export function getImageUrl(person, size = 's') { -Try replacing the `` inside `` with some text to see how the `Card` component can wrap any nested content. It doesn't need to "know" what's being rendered inside of it. You will see this flexible pattern in many places. +ລອງແທນທີ່ `` ພາຍໃນ `` ດ້ວຍຂໍ້ຄວາມເພື່ອເບິ່ງວ່າ component `Card` ສາມາດຄອບເນື້ອຫາທີ່ຊ້ອນກັນ. ມັນບໍ່ຈຳເປັນຕ້ອງ "ຮູ້" ສິ່ງທີ່ກຳລັງສະແດງພາຍໃນມັນ. ທ່ານຈະເຫັນຮູບແບບທີ່ຢືດຫຍຸ່ນນີ້ຫຼາຍບ່ອນ. + +ທ່ານສາມາດຄິດວ່າ component ທີ່ມີ prop `children` ວ່າມີ "ຂຸມ" ທີ່ສາມາດ "ເຕິມລົງ" ໂດຍ parent component ດ້ວຍ JSX ຕາມໃຈ. ທ່ານຈະໄດ້ໃຊ້ prop `children` ເລື້ອຍໆສຳລັບການຫໍ່ຫຸ້ມພາບເຊັ່ນ: panels, grids, ແລະ ອື່ນໆ. -You can think of a component with a `children` prop as having a "hole" that can be "filled in" by its parent components with arbitrary JSX. You will often use the `children` prop for visual wrappers: panels, grids, etc. -## How props change over time {/*how-props-change-over-time*/} +## props ປ່ຽນແປງແນວໃດເມື່ອເວລາຜ່ານໄປ {/*how-props-change-over-time*/} -The `Clock` component below receives two props from its parent component: `color` and `time`. (The parent component's code is omitted because it uses [state](/learn/state-a-components-memory), which we won't dive into just yet.) +Component `Clock` ດ້ານລຸ່ມຈະໄດ້ຮັບສອງ prop ຈາກ parent component ມັນເອງ: `color` ແລະ `time`. (Code ຂອງ parent component ແມ່ນຖືກປະໄວ້ເພາະວ່າມັນໃຊ້[state](/learn/state-a-components-memory), ທີ່ພວກເຮົາຍັງບໍ່ທັນເຈາະລົງເລິກໄປຕື່ມເທື່ອ.) -Try changing the color in the select box below: +ລອງປ່ຽນສີໃນ select box ດ້ານລຸ່ມ: @@ -392,7 +393,7 @@ export default function App() { return (

- Pick a color:{' '} + ເລືອກສີ:{' '}