From a70031177e53a7ea8ed19fc0cd00747a0b01849d Mon Sep 17 00:00:00 2001 From: saleumsack keoboualay Date: Wed, 24 May 2023 19:12:51 +0700 Subject: [PATCH] Add translation for /content/learn/tutorial-tic-tac-toe.md --- src/content/learn/tutorial-tic-tac-toe.md | 453 +++++++++++----------- 1 file changed, 228 insertions(+), 225 deletions(-) diff --git a/src/content/learn/tutorial-tic-tac-toe.md b/src/content/learn/tutorial-tic-tac-toe.md index 28d997b04..116a5ed05 100644 --- a/src/content/learn/tutorial-tic-tac-toe.md +++ b/src/content/learn/tutorial-tic-tac-toe.md @@ -1,31 +1,32 @@ --- -title: 'Tutorial: Tic-Tac-Toe' +title: 'ບົດສອນ: Tic-Tac-Toe' --- -You will build a small tic-tac-toe game during this tutorial. This tutorial does not assume any existing React knowledge. The techniques you'll learn in the tutorial are fundamental to building any React app, and fully understanding it will give you a deep understanding of React. +ທ່ານຈະໄດ້ເຮັດເກມ tic-tac-toe ນ້ອຍໆລະຫວ່າງບົດສອນນີ້. ບົດສອນນີ້ບໍ່ໄດ້ສົມມຸດວ່າທ່ານມີຄວາມຮູ້ກ່ຽວກັບ React ຢູ່ແລ້ວ. ເຕັກນິກທີ່ທ່ານຈະໄດ້ຮຽນໃນບົດສອນນີ້ແມ່ນພື້ນຖານການສ້າງແອັບ React, ແລະ ການທຳຄວາມເຂົ້າໃຈຢ່າງຕັ້ງໃຈມັນຈະຊ່ວຍໃຫ້ທ່ານເຂົ້າໃຈເລິງເຊິງກ່ຽວກັບ React. -This tutorial is designed for people who prefer to **learn by doing** and want to quickly try making something tangible. If you prefer learning each concept step by step, start with [Describing the UI.](/learn/describing-the-ui) +ບົດສອນນີ້ຖືກອອກແບບມາສຳລັບຄົນທີ່ມັກ **ຮຽນຮູ້ໂດຍການປະຕິບັດ** ແລະ ຕ້ອງການລອງເຮັດສິ່ງທີ່ຈັບຕ້ອງໄດ້ຢ່າງວ່ອງໄວ. ຫາກທ່ານຕ້ອງການຮຽນແຕ່ລະແນວຄິດຕາມຂັ້ນຕອນ, ເລີ່ມດ້ວຍ +[ອະທິບາຍ UI.](/learn/describing-the-ui) -The tutorial is divided into several sections: +ບົດຮຽນນີ້ແມ່ນໄດ້ແບ່ງອອກເປັນຫຼາຍສ່ວນ: -- [Setup for the tutorial](#setup-for-the-tutorial) will give you **a starting point** to follow the tutorial. -- [Overview](#overview) will teach you **the fundamentals** of React: components, props, and state. -- [Completing the game](#completing-the-game) will teach you **the most common techniques** in React development. -- [Adding time travel](#adding-time-travel) will give you **a deeper insight** into the unique strengths of React. +- [ການຕັ້ງຄ່າສຳລັບບົດຮຽນ](#setup-for-the-tutorial) ຈະໃຫ້ທ່ານເຫັນ **ຈຸດເລີ່ມຕົ້ນ** ເພື່ອປະຕິບັດຕາມບົດຮຽນ. +- [ພາບລວມ](#overview) ຈະສອນທ່ານ **ພື້ນຖານ** ຂອງ React: components, props, ແລະ state. +- [ການຈົບເກມ](#completing-the-game) ຈະສອນທ່ານ **ເຕັກນິກທີ່ພົບເລື້ອຍ** ໃນການພັດທະນາ React. +- [ການເພີ່ມ time travel](#adding-time-travel) ຈະເຮັດໃຫ້ທ່ານ **ມີຄວາມເຂົ້າໃຈເລິກເຊິງຂຶ້ນ** ໃນຈຸດແຂງທີ່ເປັນເອກະລັກຂອງ React. -### What are you building? {/*what-are-you-building*/} +### ທ່ານກຳລັງສ້າງຫຍັງ? {/*what-are-you-building*/} -In this tutorial, you'll build an interactive tic-tac-toe game with React. +ໃນບົດຮຽນນີ້, ທ່ານຈະໄດ້ສ້າງເກມ tic-tac-toe ທີ່ມີການຕອບໂຕ້ດ້ວຍ React. -You can see what it will look like when you're finished here: +ທ່ານສາມາດເບິ່ງວ່າຮູບລັກສະນະມັນຈະເປັນແບບໃດຫຼັງຈາກທີ່ເຮັດສຳເລັດແລ້ວບ່ອນນີ້: @@ -194,15 +195,15 @@ body { -If the code doesn't make sense to you yet, or if you are unfamiliar with the code's syntax, don't worry! The goal of this tutorial is to help you understand React and its syntax. +ຖ້າ code ຍັງບໍ່ສົມເຫດສົມຜົນສຳລັບທ່ານເທື່ອ, ຫຼື ຖ້າທ່ານຍັງບໍ່ທັນຄຸ້ນເຄີຍກັບ syntax ຂອງ code, ບໍ່ຕ້ອງຫ່ວງ! ເປົ້າໝາຍຂອງບົດສອນນີ້ແມ່ນຊ່ວຍໃຫ້ທ່ານມີຄວາມເຂົ້າໃຈ React ແລະ syntax ຂອງມັນ -We recommend that you check out the tic-tac-toe game above before continuing with the tutorial. One of the features that you'll notice is that there is a numbered list to the right of the game's board. This list gives you a history of all of the moves that have occurred in the game, and it is updated as the game progresses. +ພວກເຮົາແນະນຳທ່ານລອງເກມ tic-tac-toe ດ້ານເທິງກ່ອນສຶບຕໍ່ບົດສອນ. ໜຶ່ງໃນຄຸນສົມບັດຫຼັກທີ່ທ່ານສາມາດສັງເກດໄດ້ແມ່ນມີລາຍການຕົວເລກດ້ານຂວາມືຂອງກະດານ. ລາຍການນີ້ສະແດງປະຫວັດຂອງການເຄື່ອນທີ່ທັງໝົດທີ່ເກີດຂຶ້ນໃນເກມ, ແລະ ອັບເດດເມື่ອເກມດຳເນີນໄປ. -Once you've played around with the finished tic-tac-toe game, keep scrolling. You'll start with a simpler template in this tutorial. Our next step is to set you up so that you can start building the game. +ເມືອທ່ານໄດ້ຫຼິ້ນຈົນຈົບເກມ tic-tac-toe, ໃຫ້ເລື່ອນໄປເລື້ອຍໆ. ທ່ານຈະເລີ່ມຈາກ template ງ່າຍໆໃນບົດສອນນີ້. ຂັ້ນຕອນຕໍ່ໄປຂອງເຮົາແມ່ນການຕັ້ງຄ່າໃຫ້ທ່ານສາມາດເລີ່ມສ້າງເກມໄດ້. -## Setup for the tutorial {/*setup-for-the-tutorial*/} +## ການຕັ້ງຄ່າສຳລັບບົດຮຽນ {/*setup-for-the-tutorial*/} -In the live code editor below, click **Fork** in the top-right corner to open the editor in a new tab using the website CodeSandbox. CodeSandbox lets you write code in your browser and preview how your users will see the app you've created. The new tab should display an empty square and the starter code for this tutorial. +ໃນ live code editor ຄິກ **Fork** ໃນເບື້ອງຂວາທາງເທິງເພື່ອເປີດ editor ໃນແທັບໃໝ່ໂດຍໃຊ້ເວັບໄຊ CodeSandbox. CodeSandbox ໃຫ້ທ່ານຂຽນ code ໃນບາວເຊີ ແລະ ເບິ່ງຕົວຢ່າງວ່າຜູ້ໃຊ້ງານຈະເຫັນແອັບທີ່ທ່ານສ້າງຂຶ້ນແນວໃດ. ແທັບໃໝ່ຄວນຈະສະແດງປ່ອງສີ່ຫຼ່ຽມເປົ່າ ແລະ code ເລີ່ມຕົ້ນສຳລັບບົດຮຽນນີ້. @@ -261,33 +262,33 @@ body { -You can also follow this tutorial using your local development environment. To do this, you need to: +ທ່ານຍັງສາມາດເຮັດຕາມບົດສອນນີ້ໂດຍໃຊ້ local development environment ຂອງທ່ານ. ເພື່ອປະຕິບັດ, ທ່ານຕ້ອງ: -1. Install [Node.js](https://nodejs.org/en/) -1. In the CodeSandbox tab you opened earlier, press the top-left corner button to open the menu, and then choose **File > Export to ZIP** in that menu to download an archive of the files locally -1. Unzip the archive, then open a terminal and `cd` to the directory you unzipped -1. Install the dependencies with `npm install` -1. Run `npm start` to start a local server and follow the prompts to view the code running in a browser +1. ຕິດຕັ້ງ [Node.js](https://nodejs.org/en/) +1. ຢູ່ໃນແທັບ CodeSandbox ທີ່ທ່ານເປີດກ່ອນໜ້ານີ້, ກົດປຸ່ມເບື້ອງຊ້າຍເທິງເພື່ອເປີດເມນູ, ຫຼັງຈາກນັ້ນ ເລືອກ **File > Export to ZIP** ໃນເມນູນັ້ນເພື່ອດາວໂຫຼດ archive ໃນເຄື່ອງ +1. Unzip archive ຟາຍ, ຈາກນັ້ນເປີດ ແລະ `cd` ເຂົ້າໄປໃນ directory ທີ່ທ່ານ unzip. +1. ຕິດຕັ້ງ dependencies ດ້ວຍ `npm install` +1. ແລ່ນ `npm start` ເພື່ອເລີ່ມ local server ແລະ ປະຕິບັດຕາມຄຳແນະນຳເພື່ອເບິ່ງ code ທີ່ເຮັດວຽກໃນບາວເຊີ -If you get stuck, don't let this stop you! Follow along online instead and try a local setup again later. +ຖ້າທ່ານຕິດບັນຫາ, ຢ່າປ່ອຍໃຫ້ມັນຢຸດທ່ານ! ເຮັດຢູ່ອອນໄລແທນ ແລະ ລອງຕັ້ງຄ່າໃນເຄື່ອງອີກພາຍຫຼັງ. -## Overview {/*overview*/} +## ພາບລວມ {/*overview*/} -Now that you're set up, let's get an overview of React! +ປັດຈຸບັນທ່ານພ້ອມແລ້ວ, ມາເບິ່ງພາບລວມຂອງ React! -### Inspecting the starter code {/*inspecting-the-starter-code*/} +### ກວດ code ເລີ່ມຕົ້ນ {/*inspecting-the-starter-code*/} -In CodeSandbox you'll see three main sections: +ໃນ CodeSandbox ທ່ານຈະເຫັນ 3 ສ່ວນຫຼັກ: ![CodeSandbox with starter code](../images/tutorial/react-starter-code-codesandbox.png) -1. The _Files_ section with a list of files like `App.js`, `index.js`, `styles.css` and a folder called `public` -1. The _code editor_ where you'll see the source code of your selected file -1. The _browser_ section where you'll see how the code you've written will be displayed +1. ຫົວຂໍ້ _Files_ ດ້ວຍລາຍການຂອງຟາຍເຊັ່ນ: `App.js`, `index.js`, `styles.css` ແລະ ໂຟເດີທີ່ຊື່ວ່າ `public` +1. ສ່ວນ _code editor_ ບ່ອນທີ່ທ່ານຈະເຫັນ source code ຂອງຟາຍທີ່ທ່ານເລືອກ +1. ສ່ວນ _browser_ ບ່ອນທີ່ທ່ານຈະໄດ້ເຫັນ code ທີ່ທ່ານຂຽນນັ້ນສະແດງຜົນ -The `App.js` file should be selected in the _Files_ section. The contents of that file in the _code editor_ should be: +ຟາຍ `App.js` ຄວນຖືກເລືອກໃນຫົວຂໍ້ _Files_. ເນື້ອໃນຂອງຟາຍ ໃນ _code editor_ ຄວນມີດັ່ງນີ້: ```jsx export default function Square() { @@ -295,15 +296,15 @@ export default function Square() { } ``` -The _browser_ section should be displaying a square with a X in it like this: +ສ່ວນຂອງ _browser_ ຄວນສະແດງສີ່ຫຼ່ຽມພ້ອມເຄື່ອງໝາຍ X ແບບນີ້: ![x-filled square](../images/tutorial/x-filled-square.png) -Now let's have a look at the files in the starter code. +ຕອນນີ້ມາເບິ່ງ code ເລີ່ມຕົ້ນໃນຟາຍ. #### `App.js` {/*appjs*/} -The code in `App.js` creates a _component_. In React, a component is a piece of reusable code that represents a part of a user interface. Components are used to render, manage, and update the UI elements in your application. Let's look at the component line by line to see what's going on: +Code ໃນ `App.js` ສ້າງ a _component_. ໃນ React, component ແມ່ນຊີ້ນສ່ວນຂອງ code ທີ່ໃຊ້ຊໍ້າໄດ້ເຊິ່ງໄດ້ສະແດງສ່ວນໜຶ່ງຂອງ user interface . Components ແມ່ນໃຊ້ສຳລັບສະແດງ, ຈັດການ ແລະ ອັບເດດ UI element ໃນແອັບພິເຄຊັ່ນຂອງທ່ານ. ມາເບິ່ງ component ໃນແຕ່ລະແຖວວ່າມີຫຍັງແນ່: ```js {1} export default function Square() { @@ -311,23 +312,22 @@ export default function Square() { } ``` -The first line defines a function called `Square`. The `export` JavaScript keyword makes this function accessible outside of this file. The `default` keyword tells other files using your code that it's the main function in your file. +ແຖວທຳອິດປະກາດຟັງຊັ່ນຊື່ວ່າ `Square`. Keyword `export` ຂອງ JavaScript ເຮັດໃຫ້ຟັງຊັ່ນນີ້ສາມາດເຂົ້າເຖິງໄດ້ຈາກພາຍນອກຟາຍນີ້. keyword `default` ບອກຟາຍອື່ນໃຫ້ໃຊ້ code ຂອງທ່ານທີ່ເປັນຟັງຊັ່ນຫຼັກໃນຟາຍຂອງທ່ານ. ```js {2} export default function Square() { return ; } ``` - -The second line returns a button. The `return` JavaScript keyword means whatever comes after is returned as a value to the caller of the function. `` closes the JSX element to indicate that any following content shouldn't be placed inside the button. +ແຖວທີ່ສອງ return ປຸ່ມກົດ. Keyword `return` ຂອງ JavaScript ມີຄວາມໝາຍວ່າແມ່ນຫຍັງກໍຕາມທີ່ມານຳຫຼັງຈະ return ເປັນຄ່າໃຫ້ຜູ້ໃຊ້ຟັງຊັ່ນ. `` ປິດ JSX element ເພື່ອໃຫ້ຮູ້ວ່າທຸກໆເນື້ອຫາຕໍ່ໄປບໍ່ຄວນຈະຖືກວາງໄວ້ພາຍໃນປຸ່ມກົດ. #### `styles.css` {/*stylescss*/} -Click on the file labeled `styles.css` in the _Files_ section of CodeSandbox. This file defines the styles for your React app. The first two _CSS selectors_ (`*` and `body`) define the style of large parts of your app while the `.square` selector defines the style of any component where the `className` property is set to `square`. In your code, that would match the button from your Square component in the `App.js` file. +ຄິກໃສ່ຟາຍທີ່ມີຊື່ວ່າ `style.css` ໃນສ່ວນຂອງ _Files_ ຢູ່ໃນ CodeSandbox. ຟາຍນີ້ປະກາດ style ສຳລັບແອັບ React ຂອງທ່ານ. ສອງ _CSS selectors_ (`*` ແລະ `body`) ທຳອິດ ປະກາດ style ສ່ວນໃຫຍ່ຂອງແອັບທ່ານໃນຂະນະທີ່ selector `.square` ປະກາດ style ຂອງ component ທີ່ໃຊ້ property `className` ເປັນ `square`. ໃນ code ຂອງທ່ານ, ນັ້ນຈະກົງກັບປຸ່ມກົດຈາກ Component Square ໃນຟາຍ `App.js` ຂອງທ່ານ. #### `index.js` {/*indexjs*/} -Click on the file labeled `index.js` in the _Files_ section of CodeSandbox. You won't be editing this file during the tutorial but it is the bridge between the component you created in the `App.js` file and the web browser. +ຄິກໃສ່ຟາຍທີ່ມີຊື່ `index.js` ໃນສ່ວນຂອງ _Files_ ຢູ່ໃນ CodeSandbox. ທ່ານບໍ່ສາມາດແກ້ໄຂຟາຍໃນລະຫວ່າງການຮຽນນີ້ແຕ່ມັນສາມາດເຊື່ອມໂຍງ component ທີ່ທ່ານສ້າງໃນຟາຍ `App.js` ແລະ ເວັບບາວເຊີ. ```jsx import { StrictMode } from 'react'; @@ -337,20 +337,20 @@ import './styles.css'; import App from './App'; ``` -Lines 1-5 brings all the necessary pieces together: +ແຖວທີ 1-5 ແມ່ນລວມສ່ວນທີ່ຈຳເປັນມາລວມກັນ: * React -* React's library to talk to web browsers (React DOM) -* the styles for your components -* the component you created in `App.js`. +* Library React ເພື່ອສື່ສານກັບເວັບບາວເຊີ (React DOM) +* Style ຂອງ Component ທ່ານ +* Component ທີ່ທ່ານສ້າງໃນ `App.js`. -The remainder of the file brings all the pieces together and injects the final product into `index.html` in the `public` folder. +ສ່ວນທີ່ເຫຼືອຂອງຟາຍຈະເອົາຊີ້ນສ່ວນທັງໝົດມາລວມກັນ ແລະ ເຮັດເປັນ final product ລົງໃນ `index.html` ໃນໂຟນເດີ `public` -### Building the board {/*building-the-board*/} +### ການສ້າງກະດານ {/*building-the-board*/} -Let's get back to `App.js`. This is where you'll spend the rest of the tutorial. +ກັບຄືນໄປທີ່ `App.js`. ນີ້ແມ່ນບ່ອນທີ່ທ່ານຈະໃຊ້ເວລາຫຼາຍກັບບົດຮຽນ. -Currently the board is only a single square, but you need nine! If you just try and copy paste your square to make two squares like this: +ປັດຈຸບັນ ກະດານມີແຕ່ໜຶ່ງສີ່ຫຼ່ຽມ, ແຕ່ທ່ານຕ້ອງການ 9! ຖ້າທ່ານລອງແຕ່ copy paste ສີ່ຫຼ່ຽມຂອງທ່ານເພື່ອໃຫ້ເປັນສອງອັນດັ່ງນີ້: ```js {2} export default function Square() { @@ -358,7 +358,7 @@ export default function Square() { } ``` -You'll get this error: +ທ່ານຈະໄດ້ຮັບຂໍ້ຜິດພາດນີ້: @@ -366,7 +366,7 @@ You'll get this error: -React components need to return a single JSX element and not multiple adjacent JSX elements like two buttons. To fix this you can use *fragments* (`<>` and ``) to wrap multiple adjacent JSX elements like this: +Component React ຕ້ອງການ return ໜຶ່ງ JSX element ແລະ ບໍ່ສາມາດສົ່ງ JSX ທີ່ຕິດພັນຫຼາຍອັນເຊັ່ນ ສອງປຸ່ມກົດ. ເພື່ອແກ້ໄຂບັນຫານີ້ທ່ານສາມາດໃຊ້ *fragments* (`<>` ແລະ ``) ເພື່ອລວມ JSX ທີ່ຕິດພັນຫຼາຍອັນດັ່ງນີ້: ```js {3-6} export default function Square() { @@ -379,17 +379,18 @@ export default function Square() { } ``` -Now you should see: +ຕອນນີ້ທ່ານຄວນເຫັນ: ![two x-filled squares](../images/tutorial/two-x-filled-squares.png) -Great! Now you just need to copy-paste a few times to add nine squares and... +ເກັ່ງຫຼາຍ! ຕອນນີ້ທ່ານພຽງແຕ່ copy-paste ບໍ່ເທົ່າໃດເທື່ອເພື່ອເພີ່ມ 9 ສີ່ຫຼ່ຽມ ແລະ ... ![nine x-filled squares in a line](../images/tutorial/nine-x-filled-squares.png) -Oh no! The squares are all in a single line, not in a grid like you need for our board. To fix this you'll need to group your squares into rows with `div`s and add some CSS classes. While you're at it, you'll give each square a number to make sure you know where each square is displayed. -In the `App.js` file, update the `Square` component to look like this: +ຫະ! ສີ່ຫຼ່ຽມທັງໝົດແມ່ນຢູ່ແຖວດຽວກັນ, ບໍ່ໄດ້ຢູ່ໃນ grid ແບບທີ່ທ່ານຕ້ອງການຢູ່ໃນກະດານ. ເພື່ອແກ້ໄຂບັນຫານີ້ທ່ານຕ້ອງໄດ້ group ສີ່ຫຼ່ຽມໃຫ້ເປັນແຖວດ້ວຍ `
` ແລະ ເພີ່ມບາງ class CSS. ໃນຂະນະທີ່ທ່ານກຳລັງເຮັດ, ທ່ານຈະໃສ່ໂຕເລກແຕ່ລະສີ່ຫຼ່ຽມເພື່ອໃຫ້ແນ່ໃຈວ່າແຕ່ລະສີ່ຫຼ່ຽມຈະສະແດງບ່ອນໃດ. + +ໃນຟາຍ `App.js`, ອັບເດດ component `Square` ໜ້າຕາປະມານນີ້: ```js {3-19} export default function Square() { @@ -415,11 +416,11 @@ export default function Square() { } ``` -The CSS defined in `styles.css` styles the divs with the `className` of `board-row`. Now that you've grouped your components into rows with the styled `div`s you have your tic-tac-toe board: +CSS ທີ່ກຳນົດໃນ `styles.css` ແມ່ນໄດ້ style divs ດ້ວຍ `className` ຂອງ `board-row`. ຕອນນີ້ທ່ານໄດ້ group Component ຂອງທ່ານເປັນແຖວດຽວດ້ວຍ style ຂອງ div ທີ່ທ່ານມີໃນກະດານ tic-tac-toe: ![tic-tac-toe board filled with numbers 1 through 9](../images/tutorial/number-filled-board.png) -But you now have a problem. Your component named `Square`, really isn't a square anymore. Let's fix that by changing the name to `Board`: +ແຕ່ທ່ານກຳລັງມີບັນຫາ. Component ຊື່ `Square` ຂອງທ່ານ, ບໍ່ໄດ້ເປັນຮູບສີ່ຫຼ່ຽມອີກຕໍ່ໄປ. ມາແກ້ໄຂມັນໂດຍການປ່ຽນຊື່ເປັນ `Board`: ```js {1} export default function Board() { @@ -427,7 +428,7 @@ export default function Board() { } ``` -At this point your code should look something like this: +ໃນຈຸດນີ້ code ຂອງທ່ານຄວນໜ້າຕາປະມານນີ້: @@ -504,15 +505,15 @@ body { -Psssst... That's a lot to type! It's okay to copy and paste code from this page. However, if you're up for a little challenge, we recommend only copying code that you've manually typed at least once yourself. +ອືມ... ພິມໂຄດຫຼາຍ! ມັນເປັນເລື່ອງທີ່ດີທີ່ຈະ copy ແລະ paste code ຈາກ page ນີ້. ເຖິງຢ່າງໃດກໍ່ຕາມ, ຖ້າທ່ານມີຄວາມພ້ອມສຳລັບຄວາມທ້າທາຍເລັກໆນ້ອຍໆ, ພວກເຮົາຂໍແນະນຳທ່ານໃຫ້ copy code ທີ່ທ່ານພິມດ້ວຍຕົນເອງຢ່າງໜ້ອຍໜຶ່ງຄັ້ງເທົ່ານັ້ນ. -### Passing data through props {/*passing-data-through-props*/} +### ສົ່ງຂໍ້ມູນຜ່ານ props {/*passing-data-through-props*/} -Next, you'll want to change the value of a square from empty to "X" when the user clicks on the square. With how you've built the board so far you would need to copy-paste the code that updates the square nine times (once for each square you have)! Instead of copy-pasting, React's component architecture allows you to create a reusable component to avoid messy, duplicated code. +ຕໍ່ໄປ, ທ່ານຕ້ອງການປ່ຽນຄ່າຂອງສີ່ຫຼ່ຽມຈາກຄ່າເປົ່າເປັນ "X" ເມື່ອຜູ້ໃຊ້ຄິກສີ່ຫຼ່ຽມ. ໂດຍວິທີທີ່ທ່ານສ້າງກະດານຈົນມາຮອດນີ້ ທ່ານຈະຕ້ອງ copy-paste code ທີ່ອັບເດດສີ່ຫຼ່ຽມ 9 ເທື່ອ (ເຮັດເທື່ອລະສີ່ຫຼ່ຽມທີ່ທ່ານມີ)! ແທນທີ່ຈະ copy-past, ສະຖາປັກຕະຍະກຳ Component React ຊ່ວຍໃຫ້ທ່ານສ້າງ component ທີ່ໃຊ້ຊໍ້າໄດ້ເພື່ອຫຼີກຫຼ່ຽງ code ທີ່ມີຄວາມສັບສົນ, ແລະ ຊໍ້າຊ້ອນ. -First, you are going to copy the line defining your first square (``) from your `Board` component into a new `Square` component: +ທຳອິດ, ທ່ານຈະຕ້ອງ copy ແຖວທີ່ປະກາດສີ່ຫຼ່ຽມທຳອິດ (``) ຈາກ Component `Board` ໄປໃສ່ໃນ Component `Square` ໃໝ່: ```js {1-3} function Square() { @@ -524,7 +525,7 @@ export default function Board() { } ``` -Then you'll update the Board component to render that `Square` component using JSX syntax: +ຈາກນັ້ນທ່ານອັບເດດ Component Board ເພື່ອສະແດງ Component `Square` ໂດຍໃຊ້ syntax JSX: ```js {5-19} // ... @@ -551,15 +552,15 @@ export default function Board() { } ``` -Note how unlike the browser `div`s, your own components `Board` and `Square` must start with a capital letter. +ສັງເກດເຫັນຄວາມແຕກຕ່າງຂອງບາວເຊີ `div` component `Board` ແລະ `Square` ຕ້ອງເລີ່ມຕົ້ນດ້ວຍໂຕພິມໃຫຍ່. -Let's take a look: +ມາເບິ່ງ: ![one-filled board](../images/tutorial/board-filled-with-ones.png) -Oh no! You lost the numbered squares you had before. Now each square says "1". To fix this, you will use *props* to pass the value each square should have from the parent component (`Board`) to its child (`Square`). +ຫະ! ທ່ານໄດ້ເສຍຕົວເລກໃນສີ່ຫຼ່ຽມທີ່ທ່ານມີກ່ອນໜ້ານີ້. ຕອນນີ້ແຕ່ລະສີ່ຫຼ່ຽມສະແດງ "1". ເພື່ອແກ້ໄຂ, ທ່ານຈະໄດ້ໃຊ້ *props* ເພື່ອສົ່ງຜ່ານແຕ່ລະຄ່າທີ່ສີ່ຫຼ່ຽມຄວນມີຈາກ parent component (`Board`) ໄປຫາ child ຂອງມັນ (`Square`). -Update the `Square` component to read the `value` prop that you'll pass from the `Board`: +ອັບເດດ component `Square` ເືພ່ອອ່ານ `value` prop ທີ່ທ່ານຈະສົ່ງຈາກ `Board`: ```js {1} function Square({ value }) { @@ -567,9 +568,9 @@ function Square({ value }) { } ``` -`function Square({ value })` indicates the Square component can be passed a prop called `value`. +`function Square({ value })` ບົ່ງບອກວ່າ component Square ສາມາດສົ່ງ props ທີ່ເອີ້ນວ່າ `value`. -Now you want to display that `value` instead of `1` inside every square. Try doing it like this: +ຕອນນີ້ທ່ານຕ້ອງການສະແດງ `value` ນັ້ນແທນທີຈະເປັນ `1` ໃນທຸກໆສີ່ຫຼ່ຽມ. ລອງເຮັດແບບນີ້: ```js {2} function Square({ value }) { @@ -577,11 +578,11 @@ function Square({ value }) { } ``` -Oops, this is not what you wanted: +ອຸບ~~, ນີ້ບໍ່ແມ່ນສິ່ງທີ່ທ່ານຕ້ອງການ: ![value-filled board](../images/tutorial/board-filled-with-value.png) -You wanted to render the JavaScript variable called `value` from your component, not the word "value". To "escape into JavaScript" from JSX, you need curly braces. Add curly braces around `value` in JSX like so: +ທ່ານຕ້ອງການສະແດງຕົວແປ JavaScript ຊື່ວ່າ `value` ຈາກ component ຂອງທ່ານ, ບໍ່ແມ່ນຄຳວ່າ "value". ເພື່ອ "escape into JavaScriopt" ຈາກ JSX, ທ່ານຕ້ອງການວົງປີກກາ. ເພີ່ມວົງປີກກາລະຫວ່າງ `value` ໃນ JSX ແບບນີ້: ```js {2} function Square({ value }) { @@ -589,11 +590,11 @@ function Square({ value }) { } ``` -For now, you should see an empty board: +ສຳລັບຕອນນີ້, ທ່ານຄວນເຫັນກະດານທີ່ວ່າງເປົ່າ: ![empty board](../images/tutorial/empty-board.png) -This is because the `Board` component hasn't passed the `value` prop to each `Square` component it renders yet. To fix it you'll add the `value` prop to each `Square` component rendered by the `Board` component: +ນີ້ເປັນເພາະວ່າ component `Board` ບໍ່ໄດ້ສົ່ງ prop `value` ໄປໃຫ້ແຕ່ລະ component `Square` ເພື່ອສະແດງເທື່ອ. ເພື່ອແກ້ໄຂທ່ານຕ້ອງເພີ່ມ prop `value` ໃນແຕ່ລະ component `Square` ທີ່ສະແດງໂດຍ component `Board`: ```js {5-7,10-12,15-17} export default function Board() { @@ -619,11 +620,11 @@ export default function Board() { } ``` -Now you should see a grid of numbers again: +ຕອນນີ້ທ່ານຄວນເຫັນ grid ຂອງຕົວເລກອີກຄັ້ງ: ![tic-tac-toe board filled with numbers 1 through 9](../images/tutorial/number-filled-board.png) -Your updated code should look like this: +code ທີ່ອັບເດດຂອງທ່ານຄວນຄ້າຍຄືແບບນີ້: @@ -702,9 +703,9 @@ body { -### Making an interactive component {/*making-an-interactive-component*/} +### ການເຮັດໃຫ້ Component ມີການໂຕ້ຕອບ {/*making-an-interactive-component*/} -Let's fill the `Square` component with an `X` when you click it. Declare a function called `handleClick` inside of the `Square`. Then, add `onClick` to the props of the button JSX element returned from the `Square`: +ມາເຕີມ component `Square` ດ້ວຍ `X` ເມື່ອທ່ານຄິກມັນ. ປະກາດຟັງຊັ່ນຊື່ວ່າ `handleClick` ພາຍໃນ `Square`. ຫຼັງຈາກນັ້ນ, ເພີ່ມ `onClick` ໃສ່ props ຂອງປຸ່ມກົດ JSX element ທີ່ return ຈາກ `Square`: ```js {2-4,9} function Square({ value }) { @@ -723,19 +724,19 @@ function Square({ value }) { } ``` -If you click on a square now, you should see a log saying `"clicked!"` in the _Console_ tab at the bottom of the _Browser_ section in CodeSandbox. Clicking the square more than once will log `"clicked!"` again. Repeated console logs with the same message will not create more lines in the console. Instead, you will see an incrementing counter next to your first `"clicked!"` log. +ຖ້າທ່ານຄິກໃສ່ສີ່ຫຼ່ຽມຕອນນີ້, ທ່ານຄວນຈະເຫັນ log ທີ່ສະແດງ `"clicked!"` ໃນແທັບ _Console_ ທີ່ຢູ່ດ້ານລຸ່ມຂອງ _Browser_ ໃນ CodeSandbox. ການຄິກສີ່ຫຼ່ຽມຫຼາຍກວ່າໜຶ່ງຄັ້ງຈະສະແດງ `"clicked!"` ອີກຄັ້ງ. ການເຮັດຊໍ້າ console log ດ້ວຍຂໍ້ຄວາມເກົ່າຈະບໍ່ສ້າງແຖວໃໝ່ໃນ console. ກົງກັນຂ້າມ, ທ່ານຈະເຫັນຕົວເລກການເພີ່ມຂຶ້ນດ້ານຫຼັງຂອງຄຳວ່າ `"clicked!"`. -If you are following this tutorial using your local development environment, you need to open your browser's Console. For example, if you use the Chrome browser, you can view the Console with the keyboard shortcut **Shift + Ctrl + J** (on Windows/Linux) or **Option + ⌘ + J** (on macOS). +ຖ້າທ່ານປະຕິບັດຕາມບົດຮຽນໂດຍໃຊ້ local development environment, ທ່ານຕ້ອງເປີດບາວເຊີ Console ຂອງທ່ານ. ຕົວຢ່າງ, ຖ້າທ່ານໃຊ້ Chrome ບາວເຊີ, ທ່ານສາມາດເບິ່ງ Console ດ້ວຍ keyboard shortcut **Shift + Ctrl + J** (ສຳລັບ Windows/Linux) ຫຼື **Option + ⌘ + J** (ສຳລັບ macOS). -As a next step, you want the Square component to "remember" that it got clicked, and fill it with an "X" mark. To "remember" things, components use *state*. +ໃນຂັ້ນຕອນຕໍ່ໄປ, ທ່ານຕ້ອງການໃຫ້ component Square ເພື່ອ "ຈື່" ວ່າມັນໄດ້ຖືກຄິກແລ້ວ, ແລະ ເຕີມມັນດ້ວຍເຄື່ອງໝາຍ "X". ເພື່ອ "ຈື່" ຄ່າຕ່າງໆ, comopnent ໃຊ້ *state*. -React provides a special function called `useState` that you can call from your component to let it "remember" things. Let's store the current value of the `Square` in state, and change it when the `Square` is clicked. +React ມີຟັງຊັ່ນພິເສດເອີ້ນວ່າ `useState` ທີ່ທ່ານສາມາດເອີ້ນຈາກ component ຂອງທ່ານເພື່ອໃຫ້ "ຈື່" ຄ່າ. ມາເກັບຄ່າຂອງ `Square` ໃນ state, ແລະ ປ່ຽນມັນເມື່ອ `Square` ຖືກຄິກ. -Import `useState` at the top of the file. Remove the `value` prop from the `Square` component. Instead, add a new line at the start of the `Square` that calls `useState`. Have it return a state variable called `value`: +Import `useState` ເທິງສຸດຂອງຟາຍ. ລຶບ prop `value` ຈາກ component `Square`. ກົງກັນຂ້າມ, ເພີ່ມແຖວໃໝ່ດ້ານໜ້າຂອງ `Square` ທີ່ເອີ້ນ `useSate`. ໃຫ້ສົ່ງຄືນຕົວແປທີ່ຊື່ `value`: ```js {1,3,4} import { useState } from 'react'; @@ -747,9 +748,9 @@ function Square() { //... ``` -`value` stores the value and `setValue` is a function that can be used to change the value. The `null` passed to `useState` is used as the initial value for this state variable, so `value` here starts off equal to `null`. +`value` ເກັບຄ່າ ແລະ `setValue` ແມ່ນຟັງຊັ່ນທີ່ໃຊ້ເພື່ອປ່ຽນແປງຄ່າ. ຄ່າ `null` ທີ່ສົ່ງຜ່ານໄປຫາ `useState` ເພື່ອໃຊ້ເປັນຄ່າເລີ່ມຕົ້ນຂອງຕົວແປ state ນີ້, ສະນັ້ນ `value` ມີຄ່າເລີ່ມຕົ້ນເປັນ `null`. -Since the `Square` component no longer accepts props anymore, you'll remove the `value` prop from all nine of the Square components created by the Board component: +ຕັ້ງແຕ່ component `Square` ບໍ່ໄດ້ຮັບ prop ອີກຕໍ່ໄປ, ທ່ານຈະໄດ້ລຶບ prop `value` ຈາກ 9 component Square ທີ່ຖືກສ້າງໂດຍ component Board: ```js {6-8,11-13,16-18} // ... @@ -776,7 +777,7 @@ export default function Board() { } ``` -Now you'll change `Square` to display an "X" when clicked. Replace the `console.log("clicked!");` event handler with `setValue('X');`. Now your `Square` component looks like this: +ຕອນນີ້ທ່ານຈະໄດ້ປ່ຽນ `Square` ເພື່ອສະແດງ "X" ຕອນຖືກຄິກ. ແທນທີ `console.log("clicked!");` event handler ດ້ວຍ `setValue('X');`. ປັດຈຸບັນ component `Square` ຈະໜ້າຕາປະມານນີ້: ```js {5} function Square() { @@ -797,13 +798,14 @@ function Square() { } ``` -By calling this `set` function from an `onClick` handler, you're telling React to re-render that `Square` whenever its `