From 662d619aa0a3f3d157569f119502399ca2755117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rado=C5=A1=20Mili=C4=87ev?= <40705899+rammba@users.noreply.github.com> Date: Mon, 16 Dec 2024 20:33:07 +0100 Subject: [PATCH 1/2] Translate state-a-components-memory.md --- src/content/learn/adding-interactivity.md | 4 +- src/content/learn/editor-setup.md | 2 +- src/content/learn/index.md | 2 +- .../learn/state-a-components-memory.md | 318 +++++++++--------- 4 files changed, 163 insertions(+), 163 deletions(-) diff --git a/src/content/learn/adding-interactivity.md b/src/content/learn/adding-interactivity.md index 128b330f..91ba1205 100644 --- a/src/content/learn/adding-interactivity.md +++ b/src/content/learn/adding-interactivity.md @@ -68,11 +68,11 @@ button { margin-right: 10px; } -Pročitajte **[Odgovaranje na Event-e](/learn/responding-to-events)** da biste naučili kako da dodate event handler-e. +Pročitajte **[Odgovaranje na event-e](/learn/responding-to-events)** da biste naučili kako da dodate event handler-e. -## State: memorija component-e {/*state-a-components-memory*/} +## State: Memorija component-e {/*state-a-components-memory*/} Component-e često moraju da promene ono što je prikazano na ekranu kao rezultat neke interakcije. Kucanje u formu treba da update-uje polje za unos, klik na "sledeće" u karuselu slika treba da promeni prikazanu sliku, klik na "kupi" stavlja proizvod u korpu. Component-e moraju da "pamte" određene stvari: trenutnu vrednost unosa, trenutnu sliku, korpu za kupovinu. U React-u, ova vrsta memorije specifična za component-e naziva se *state.* diff --git a/src/content/learn/editor-setup.md b/src/content/learn/editor-setup.md index 404514bc..4f9909d5 100644 --- a/src/content/learn/editor-setup.md +++ b/src/content/learn/editor-setup.md @@ -33,7 +33,7 @@ Neki editori dolaze sa ugrađenim funkcijama, dok drugi zahtevaju dodavanje ekst Linting alati pronalaze probleme u vašem kodu dok ga pišete, pomažući vam da ih ispravite na vreme. [ESLint](https://eslint.org/) je popularan open source linter za JavaScript. -* [Instalirajte ESLint sa preporučenom konfiguracijom za React](https://www.npmjs.com/package/eslint-config-react-app) (budite sigurni da imate [Node instaliran!](https://nodejs.org/en/download/current/)) +* [Instalirajte ESLint sa preporučenom konfiguracijom za React](https://www.npmjs.com/package/eslint-config-react-app) (budite sigurni da imate [instaliran Node](https://nodejs.org/en/download/current/)!) * [Integrišite ESLint u VSCode sa zvaničnom ekstenzijom](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) **Budite sigurni da ste omogućili sva [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks) pravila za vaš projekat.** Oni su esencijalni i hvataju najozbiljnije greške na vreme. Preporučena [`eslint-config-react-app`](https://www.npmjs.com/package/eslint-config-react-app) konfiguracija već ih uključuje. diff --git a/src/content/learn/index.md b/src/content/learn/index.md index 40eae1a7..a45b1ee3 100644 --- a/src/content/learn/index.md +++ b/src/content/learn/index.md @@ -316,7 +316,7 @@ function MyButton() { // ... ``` -Od `useState` dobićete dve stvari: trenutni state (`count`), i funkciju koja vam omogućava da ga ažurirate (`setCount`). Možete im dati bilo koja imena, ali je konvencija da pišete `[nešto, setNešto]`. +Od `useState` dobićete dve stvari: trenutni state (`count`), i funkciju koja vam omogućava da ga ažurirate (`setCount`). Možete im dati bilo koja imena, ali je konvencija da pišete `[something, setSomething]`. Prvi put kada se dugme prikaže, `count` će biti `0` jer ste `0` prosledili u `useState()`. Kada želite da promenite state, pozovite `setCount()` i prosledite joj novu vrednost. Klikom na ovo dugme inkrementujte brojač: diff --git a/src/content/learn/state-a-components-memory.md b/src/content/learn/state-a-components-memory.md index 75a1fd0b..0d2fd910 100644 --- a/src/content/learn/state-a-components-memory.md +++ b/src/content/learn/state-a-components-memory.md @@ -1,25 +1,25 @@ --- -title: "State: A Component's Memory" +title: "State: Memorija komponente" --- -Components often need to change what's on the screen as a result of an interaction. Typing into the form should update the input field, clicking "next" on an image carousel should change which image is displayed, clicking "buy" should put a product in the shopping cart. Components need to "remember" things: the current input value, the current image, the shopping cart. In React, this kind of component-specific memory is called *state*. +Komponente često trebaju promeniti prikaz na ekranu u zavisnosti od interakcija. Pisanje u formu bi trebalo da osveži input polje, klik na "naredno" u carousel-u za slike treba promeniti prikazanu sliku, klik na "kupi" bi trebao da stavi proizvod u korpu. Komponente trebaju da "pamte" stvari: trenutnu vrednost input-a, trenutnu sliku, korpu sa proizvodima. U React-u, ova vrsta memorije koja je specifična za komponentu se naziva *state*. -* How to add a state variable with the [`useState`](/reference/react/useState) Hook -* What pair of values the `useState` Hook returns -* How to add more than one state variable -* Why state is called local +* Kako da dodate state promenljivu sa [`useState`](/reference/react/useState) Hook-om +* Koji par vrednosti vraća `useState` Hook +* Kako da dodate više od jedne state promenljive +* Zašto se state naziva lokalnim -## When a regular variable isn’t enough {/*when-a-regular-variable-isnt-enough*/} +## Kad obična promenljiva nije dovoljna {/*when-a-regular-variable-isnt-enough*/} -Here's a component that renders a sculpture image. Clicking the "Next" button should show the next sculpture by changing the `index` to `1`, then `2`, and so on. However, this **won't work** (you can try it!): +Ovde je komponenta koja renderuje sliku skulpture. Klikom na dugme "Naredno" trebala bi se prikazati naredna skulptura promenom `index` promenljive na `1`, pa na `2` i tako dalje. Međutim, ovo **neće raditi** (možete probati!): @@ -37,14 +37,14 @@ export default function Gallery() { return ( <>

{sculpture.name} - by {sculpture.artist} + napravio/la {sculpture.artist}

- ({index + 1} of {sculptureList.length}) + ({index + 1} od {sculptureList.length})

-The `handleClick` event handler is updating a local variable, `index`. But two things prevent that change from being visible: +`handleClick` event handler ažurira lokalnu promenljivu `index`. Ali, dve stvari sprečavaju tu promenu da bude primenjena: -1. **Local variables don't persist between renders.** When React renders this component a second time, it renders it from scratch—it doesn't consider any changes to the local variables. -2. **Changes to local variables won't trigger renders.** React doesn't realize it needs to render the component again with the new data. +1. **Lokalne promenljive se ne čuvaju između rendera.** Kada React renderuje komponentu drugi put, renderuje je iznova—ne uzima u obzir nikakve promene u lokalnim promenljivama. +2. **Promene lokalnih promenljiva neće okinuti renderovanje.** React ne shvata da treba da renderuje komponentu ponovo sa novim podacima. -To update a component with new data, two things need to happen: +Da biste ažurirali komponentu sa novim podacima, potrebne su dve stvari: -1. **Retain** the data between renders. -2. **Trigger** React to render the component with new data (re-rendering). +1. **Zadržavanje** podataka između rendera. +2. **Okidanje** renderovanja komponente sa novim podacima (ponovno renderovanje). -The [`useState`](/reference/react/useState) Hook provides those two things: +[`useState`](/reference/react/useState) Hook omogućava te dve stvari: -1. A **state variable** to retain the data between renders. -2. A **state setter function** to update the variable and trigger React to render the component again. +1. **state promenljiva** zadržava podatke između rendera. +2. **state setter funkcija** ažurira promenljivu i tera React da okine renderovanje komponente ponovo. -## Adding a state variable {/*adding-a-state-variable*/} +## Dodavanje state promenljive {/*adding-a-state-variable*/} -To add a state variable, import `useState` from React at the top of the file: +Da bi ste dodali state promenljivu, import-ujte `useState` iz React-a na vrhu fajla: ```js import { useState } from 'react'; ``` -Then, replace this line: +Onda, zamenite ovu liniju: ```js let index = 0; ``` -with +sa ```js const [index, setIndex] = useState(0); ``` -`index` is a state variable and `setIndex` is the setter function. +`index` je state promenljiva, a `setIndex` je setter funkcija. -> The `[` and `]` syntax here is called [array destructuring](https://javascript.info/destructuring-assignment) and it lets you read values from an array. The array returned by `useState` always has exactly two items. +> Sintaksa sa `[` i `]` se naziva [dekonstruisanje niza](https://javascript.info/destructuring-assignment) i omogućava vam da čitate vrednosti iz niza. Niz koji `useState` vraća uvek ima tačno dva člana. -This is how they work together in `handleClick`: +Evo kako rade zajedno u `handleClick`: ```js function handleClick() { @@ -198,7 +198,7 @@ function handleClick() { } ``` -Now clicking the "Next" button switches the current sculpture: +Sada, klikom na dugme "Naredno" promeniće se odabrana skulptura: @@ -217,14 +217,14 @@ export default function Gallery() { return ( <>

{sculpture.name} - by {sculpture.artist} + napravio/la {sculpture.artist}

- ({index + 1} of {sculptureList.length}) + ({index + 1} od {sculptureList.length})

-### Meet your first Hook {/*meet-your-first-hook*/} +### Upoznajte svoj prvi Hook {/*meet-your-first-hook*/} -In React, `useState`, as well as any other function starting with "`use`", is called a Hook. +U React-u, `useState`, kao i ostale funkcije koje počinju sa "`use`", naziva se Hook. -*Hooks* are special functions that are only available while React is [rendering](/learn/render-and-commit#step-1-trigger-a-render) (which we'll get into in more detail on the next page). They let you "hook into" different React features. +*Hook-ovi* su specijalne funkcije koje su dostupne isključivo u toku React [renderovanja](/learn/render-and-commit#step-1-trigger-a-render) (što ćemo objasniti detaljnije na narednoj stranici). Omogućavaju vam da "se zakačite" za različite funkcionalnosti React-a. -State is just one of those features, but you will meet the other Hooks later. +State je samo jedna od tih funkcionalnosti, ali ćete upoznati i ostale Hook-ove kasnije. -**Hooks—functions starting with `use`—can only be called at the top level of your components or [your own Hooks.](/learn/reusing-logic-with-custom-hooks)** You can't call Hooks inside conditions, loops, or other nested functions. Hooks are functions, but it's helpful to think of them as unconditional declarations about your component's needs. You "use" React features at the top of your component similar to how you "import" modules at the top of your file. +**Hook-ovi—funkcije koje počinju sa `use`—mogu biti pozvane samo na početku vaših komponenata ili [vaših Hooks-ova](/learn/reusing-logic-with-custom-hooks)**. Ne možete pozivati Hook-ove unutar uslova, petlji, ili drugih ugnježdenih funkcija. Hook-ovi su funkcije, ali je korisno da na njih gledate kao bezuslovne deklaracije onoga što je komponenti potrebno. "Koristite" React funkcionalnosti na vrhu komponente slično kao što "import-ujete" module na vrhu fajla. -### Anatomy of `useState` {/*anatomy-of-usestate*/} +### Anatomija `useState`-a {/*anatomy-of-usestate*/} -When you call [`useState`](/reference/react/useState), you are telling React that you want this component to remember something: +Kada pozovete [`useState`](/reference/react/useState), govorite React-u da želite da ta komponenta nešto zapamti: ```js const [index, setIndex] = useState(0); ``` -In this case, you want React to remember `index`. +U ovom slučaju, želite da React zapamti `index`. -The convention is to name this pair like `const [something, setSomething]`. You could name it anything you like, but conventions make things easier to understand across projects. +Konvencija je da se par nazove ovako `const [something, setSomething]`. Možete staviti bilo koja imena, ali konvencije čine kod lakšim za razumevanje na različitim projektima. -The only argument to `useState` is the **initial value** of your state variable. In this example, the `index`'s initial value is set to `0` with `useState(0)`. +Jedini argument u `useState` je **inicijalna vrednost** vaše state promenljive. U ovom primeru, inicijalna vrednost `index`-a je postavljena na `0` sa `useState(0)`. -Every time your component renders, `useState` gives you an array containing two values: +Svaki put kad se komponenta renderuje, `useState` vam daje niz koji sadrži dve vrednosti: -1. The **state variable** (`index`) with the value you stored. -2. The **state setter function** (`setIndex`) which can update the state variable and trigger React to render the component again. +1. **state promenljiva** (`index`) sa vrednošću koju ste sačuvali. +2. **state setter funkcija** (`setIndex`) koja može da ažurira state promenljivu i natera React da okine renderovanje komponente ponovo. -Here's how that happens in action: +Evo toga i u praksi: ```js const [index, setIndex] = useState(0); ``` -1. **Your component renders the first time.** Because you passed `0` to `useState` as the initial value for `index`, it will return `[0, setIndex]`. React remembers `0` is the latest state value. -2. **You update the state.** When a user clicks the button, it calls `setIndex(index + 1)`. `index` is `0`, so it's `setIndex(1)`. This tells React to remember `index` is `1` now and triggers another render. -3. **Your component's second render.** React still sees `useState(0)`, but because React *remembers* that you set `index` to `1`, it returns `[1, setIndex]` instead. -4. And so on! +1. **Vaša komponenta se renderuje prvi put.** Pošto ste prosledili `0` u `useState` kao inicijalnu vrednost za `index`, vratiće `[0, setIndex]`. React pamti da je `0` poslednja state vrednost. +2. **Ažurirate state.** Kada korisnik klikne na dugme poziva se `setIndex(index + 1)`. `index` je `0`, tako da je to `setIndex(1)`. Ovo govori React-u da zapamti da je `index` sada `1` i okida novi render. +3. **Drugo renderovanje vaše komponente.** React i dalje vidi `useState(0)`, ali zato što React *pamti* da ste setovali `index` na `1`, vratiće `[1, setIndex]`. +4. I tako dalje! -## Giving a component multiple state variables {/*giving-a-component-multiple-state-variables*/} +## Davanje komponenti više state promenljivih {/*giving-a-component-multiple-state-variables*/} -You can have as many state variables of as many types as you like in one component. This component has two state variables, a number `index` and a boolean `showMore` that's toggled when you click "Show details": +Možete imati koliko god state promenljivih sa koliko god tipova podataka želite u jednoj komponenti. Ova komponenta ima dve state promenljive, broj `index` i boolean `showMore` koji se menja klikom na "Prikaži detalje": @@ -405,17 +405,17 @@ export default function Gallery() { return ( <>

{sculpture.name} - by {sculpture.artist} + napravio/la {sculpture.artist}

- ({index + 1} of {sculptureList.length}) + ({index + 1} od {sculptureList.length})

{showMore &&

{sculpture.description}

} -It is a good idea to have multiple state variables if their state is unrelated, like `index` and `showMore` in this example. But if you find that you often change two state variables together, it might be easier to combine them into one. For example, if you have a form with many fields, it's more convenient to have a single state variable that holds an object than state variable per field. Read [Choosing the State Structure](/learn/choosing-the-state-structure) for more tips. +Dobra je ideja imati više state promenljivih ako im state-ovi nisu povezani, kao `index` i `showMore` u ovom primeru. Ali, ako primetite da često menjate dve state promenljive zajedno, može biti lakše da ih grupišete u jednu. Na primer, ako imate formu sa mnogo polja, zgodnije je imati jednu state promenljivu koja drži ceo objekat, umesto jedne state promenljive po polju. Pročitajte [Odabir state strukture](/learn/choosing-the-state-structure) za više detalja. -#### How does React know which state to return? {/*how-does-react-know-which-state-to-return*/} +#### Kako React zna koji state da vrati? {/*how-does-react-know-which-state-to-return*/} -You might have noticed that the `useState` call does not receive any information about *which* state variable it refers to. There is no "identifier" that is passed to `useState`, so how does it know which of the state variables to return? Does it rely on some magic like parsing your functions? The answer is no. +Možda ste već primetili da `useState` poziv ne prima informaciju o tome na *koju* state promenljivu se odnosi. Ne postoji "identifikator" koji se prosleđuje u `useState`, pa kako onda zna koju od state promenljivih da vrati? Da li se oslanja na neku magiju poput parsiranja vaših funkcija? Odgovor je ne. -Instead, to enable their concise syntax, Hooks **rely on a stable call order on every render of the same component.** This works well in practice because if you follow the rule above ("only call Hooks at the top level"), Hooks will always be called in the same order. Additionally, a [linter plugin](https://www.npmjs.com/package/eslint-plugin-react-hooks) catches most mistakes. +Umesto toga, da bi omogućili svoju konciznu sintaksu, Hook-ovi **se oslanjaju na stabilan redosled poziva pri svakom renderovanju jedne komponente**. Ovo radi veoma dobro u praksi, jer ako pratite pravilo od gore ("pozivajte Hook-ove samo na početku"), Hook-ovi će uvek biti pozvani u istom redosledu. Dodatno, [linter plugin](https://www.npmjs.com/package/eslint-plugin-react-hooks) hvata većinu grešaka. -Internally, React holds an array of state pairs for every component. It also maintains the current pair index, which is set to `0` before rendering. Each time you call `useState`, React gives you the next state pair and increments the index. You can read more about this mechanism in [React Hooks: Not Magic, Just Arrays.](https://medium.com/@ryardley/react-hooks-not-magic-just-arrays-cd4f1857236e) +Interno, React čuva niz state parova za svaku komponentu. Takođe održava i trenutni indeks para koji je postavljen na `0` pre renderovanja. Svaki put kad pozovete `useState`, React vam daje naredni state par i povećava indeks. Možete pročitati više o ovom mehanizmu u [React Hook-ovi: Nije magija, već samo nizovi](https://medium.com/@ryardley/react-hooks-not-magic-just-arrays-cd4f1857236e). -This example **doesn't use React** but it gives you an idea of how `useState` works internally: +Ovaj primer **ne koristi React**, ali vam daje ideju kako `useState` zapravo radi: @@ -540,37 +540,37 @@ This example **doesn't use React** but it gives you an idea of how `useState` wo let componentHooks = []; let currentHookIndex = 0; -// How useState works inside React (simplified). +// Kako useState radi unutar React-a (pojednostavljeno). function useState(initialState) { let pair = componentHooks[currentHookIndex]; if (pair) { - // This is not the first render, - // so the state pair already exists. - // Return it and prepare for next Hook call. + // Ovo nije prvi render, + // tako da state par već postoji. + // Vrati ga i pripremi se za naredni Hook poziv. currentHookIndex++; return pair; } - // This is the first time we're rendering, - // so create a state pair and store it. + // Ovo je prvi put da renderujemo, + // tako da kreiraj state par i sačuvaj ga. pair = [initialState, setState]; function setState(nextState) { - // When the user requests a state change, - // put the new value into the pair. + // Kada korisnik zatraži promenu state-a, + // postavi novu vrednost u par. pair[0] = nextState; updateDOM(); } - // Store the pair for future renders - // and prepare for the next Hook call. + // Sačuvaj par za naredna renderovanja + // i pripremi se za naredni Hook poziv. componentHooks[currentHookIndex] = pair; currentHookIndex++; return pair; } function Gallery() { - // Each useState() call will get the next pair. + // Svaki useState() poziv će dobiti naredni par. const [index, setIndex] = useState(0); const [showMore, setShowMore] = useState(false); @@ -583,14 +583,14 @@ function Gallery() { } let sculpture = sculptureList[index]; - // This example doesn't use React, so - // return an output object instead of JSX. + // Ovaj primer ne koristi React, pa + // vrati objekat umesto JSX-a kao rezultat. return { onNextClick: handleNextClick, onMoreClick: handleMoreClick, - header: `${sculpture.name} by ${sculpture.artist}`, - counter: `${index + 1} of ${sculptureList.length}`, - more: `${showMore ? 'Hide' : 'Show'} details`, + header: `${sculpture.name} napravio/la ${sculpture.artist}`, + counter: `${index + 1} od ${sculptureList.length}`, + more: `${showMore ? 'Sakrij' : 'Prikaži'} detalje`, description: showMore ? sculpture.description : null, imageSrc: sculpture.url, imageAlt: sculpture.alt @@ -598,13 +598,13 @@ function Gallery() { } function updateDOM() { - // Reset the current Hook index - // before rendering the component. + // Resetuj trenutni indeks Hook-a + // pre renderovanja komponente. currentHookIndex = 0; let output = Gallery(); - // Update the DOM to match the output. - // This is the part React does for you. + // Osveži DOM da se poklapa sa rezultatom. + // Ovo je deo koji React radi za vas. nextButton.onclick = output.onNextClick; header.textContent = output.header; moreButton.onclick = output.onMoreClick; @@ -698,13 +698,13 @@ let sculptureList = [{ alt: 'A group of bronze hippo sculptures emerging from the sett sidewalk as if they were swimming.' }]; -// Make UI match the initial state. +// Učini da UI prikazuje inicijalni state. updateDOM(); ``` ```html public/index.html @@ -724,15 +724,15 @@ button { display: block; margin-bottom: 10px; } -You don't have to understand it to use React, but you might find this a helpful mental model. +Ne morate ovo razumeti da bi koristili React, ali vam može koristiti kao mentalni model. -## State is isolated and private {/*state-is-isolated-and-private*/} +## State je izolovan i privatan {/*state-is-isolated-and-private*/} -State is local to a component instance on the screen. In other words, **if you render the same component twice, each copy will have completely isolated state!** Changing one of them will not affect the other. +State je lokalan za instancu komponente na ekranu. Drugim rečima, **ako jednu komponentu renderujete dvaput, svaka će imati potpuno izolovan state**! Promenom jednog nećete uticati na drugi. -In this example, the `Gallery` component from earlier is rendered twice with no changes to its logic. Try clicking the buttons inside each of the galleries. Notice that their state is independent: +U ovom primeru, `Gallery` komponenta od ranije je renderovana dvaput bez promena u njenoj logici. Probajte da kliknete dugmiće u svakoj galeriji. Primetite da su njihovi state-ovi nezavisni: @@ -770,17 +770,17 @@ export default function Gallery() { return (

{sculpture.name} - by {sculpture.artist} + napravio/la {sculpture.artist}

- ({index + 1} of {sculptureList.length}) + ({index + 1} od {sculptureList.length})

{showMore &&

{sculpture.description}

} -This is what makes state different from regular variables that you might declare at the top of your module. State is not tied to a particular function call or a place in the code, but it's "local" to the specific place on the screen. You rendered two `` components, so their state is stored separately. +Ovo je ono što state čini drugačijim od običnih promenljivih koje biste deklarisali na vrhu modula. State nije vezan za određeni poziv funkcije niti za mesto u kodu, već je "lokalan" za specifično mesto na ekranu. Renderovali ste dve `` komponente, pa im se state čuva odvojeno. -Also notice how the `Page` component doesn't "know" anything about the `Gallery` state or even whether it has any. Unlike props, **state is fully private to the component declaring it.** The parent component can't change it. This lets you add state to any component or remove it without impacting the rest of the components. +Primetite i da `Page` komponenta ne "zna" ništa o `Gallery` state-u niti da li on uopšte postoji. Za razliku od props-a, **state je potpuno privatan u komponenti koja ga deklariše**. Roditeljska komponenta ga ne može promeniti. Ovo vam omogućava da dodate ili uklonite state iz neke komponente bez uticaja na ostale komponente. -What if you wanted both galleries to keep their states in sync? The right way to do it in React is to *remove* state from child components and add it to their closest shared parent. The next few pages will focus on organizing state of a single component, but we will return to this topic in [Sharing State Between Components.](/learn/sharing-state-between-components) +Šta ako poželite da obe galerije sinhronizuju state? Pravilan način da to uradite u React-u je da *uklonite* state iz dečjih komponenata i dodate ga u najbližeg zajedničkog roditelja. Narednih nekoliko stranica će se fokusirati na organizaciju state-a u jednoj komponenti, ali vratićemo se na ovu temu u [Podela state-a među komponentama](/learn/sharing-state-between-components). -* Use a state variable when a component needs to "remember" some information between renders. -* State variables are declared by calling the `useState` Hook. -* Hooks are special functions that start with `use`. They let you "hook into" React features like state. -* Hooks might remind you of imports: they need to be called unconditionally. Calling Hooks, including `useState`, is only valid at the top level of a component or another Hook. -* The `useState` Hook returns a pair of values: the current state and the function to update it. -* You can have more than one state variable. Internally, React matches them up by their order. -* State is private to the component. If you render it in two places, each copy gets its own state. +* Koristite state promenljivu kada komponenta treba da "upamti" neke informacije između rendera. +* State promenljive su deklarisane pozivanjem `useState` Hook-a. +* Hook-ovi su specijalne funkcije koje počinju sa `use`. Omogućavaju vam da "se zakačite" za React funkcionalnosti poput state-a. +* Hook-ovi mogu podsećati na import-e: moraju biti pozvani bezuslovno. Pozivanje Hook-ova, uključujući i `useState`, dozvoljeno je samo na početku komponenata ili drugog Hook-a. +* `useState` Hook vraća par vrednosti: trenutni state i funkciju koja ga ažurira. +* Možete imati više od jedne state promenljive. Interno, React ih prepoznaje po redosledu. +* State je privatan za komponentu. Ako je renderujete na dva mesta, svaka će imati svoj state. @@ -913,11 +913,11 @@ What if you wanted both galleries to keep their states in sync? The right way to -#### Complete the gallery {/*complete-the-gallery*/} +#### Kompletirati galeriju {/*complete-the-gallery*/} -When you press "Next" on the last sculpture, the code crashes. Fix the logic to prevent the crash. You may do this by adding extra logic to event handler or by disabling the button when the action is not possible. +Kada pritisnete "Naredno" na poslednjoj skulpturi, kod prestaje da radi. Popravite logiku da biste to sprečili. Možete ovo rešiti dodavanjem dodatne logike u event handler ili onemogućavanjem dugmeta kada akcija nije moguća. -After fixing the crash, add a "Previous" button that shows the previous sculpture. It shouldn't crash on the first sculpture. +Kada ovo popravite, dodajte dugme "Prethodno" koje prikazuje prethodnu skulpturu. Ne bi trebalo da se skrši na prvoj skulpturi. @@ -941,17 +941,17 @@ export default function Gallery() { return ( <>

{sculpture.name} - by {sculpture.artist} + napravio/la {sculpture.artist}

- ({index + 1} of {sculptureList.length}) + ({index + 1} od {sculptureList.length})

{showMore &&

{sculpture.description}

} -This adds a guarding condition inside both event handlers and disables the buttons when needed: +Ovo dodaje zaštitni uslov unutar oba event handler-a i onemogućava dugmiće kad je potrebno: @@ -1097,23 +1097,23 @@ export default function Gallery() { onClick={handlePrevClick} disabled={!hasPrev} > - Previous + Prethodno

{sculpture.name} - by {sculpture.artist} + napravio/la {sculpture.artist}

- ({index + 1} of {sculptureList.length}) + ({index + 1} od {sculptureList.length})

{showMore &&

{sculpture.description}

} -Notice how `hasPrev` and `hasNext` are used *both* for the returned JSX and inside the event handlers! This handy pattern works because event handler functions ["close over"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures) any variables declared while rendering. +Primetite da su `hasPrev` i `hasNext` korišćeni na *dva* mesta, u povratnom JSX-u i unutar event handler-a! Ovaj zgodni šablon radi zato što event handler funkcije ["zatvaraju"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures) sve promenljive deklarisane tokom renderovanja. -#### Fix stuck form inputs {/*fix-stuck-form-inputs*/} +#### Popraviti zaglavljene input-e forme {/*fix-stuck-form-inputs*/} -When you type into the input fields, nothing appears. It's like the input values are "stuck" with empty strings. The `value` of the first `` is set to always match the `firstName` variable, and the `value` for the second `` is set to always match the `lastName` variable. This is correct. Both inputs have `onChange` event handlers, which try to update the variables based on the latest user input (`e.target.value`). However, the variables don't seem to "remember" their values between re-renders. Fix this by using state variables instead. +Kada kucate u input polja, ništa se ne prikazuje. Kao da su input vrednosti "zaglavljene" sa praznim stringovima. `value` prvog ``-a je setovan da uvek odgovara `firstName` promenljivoj, a `value` drugog ``-a je setovan da uvek odgovara `lastName` promenljivoj. Ovo je tačno. Oba input-a imaju `onChange` event handler-e koji pokušavaju da ažuriraju promenljive na osnovu poslednje vrednosti koju je korisnik uneo (`e.target.value`). Međutim, deluje da promenljive ne "pamte" svoje vrednosti između rendera. Popravite ovo korišćenjem state promenljivih. @@ -1250,17 +1250,17 @@ export default function Form() { return (
e.preventDefault()}> -

Hi, {firstName} {lastName}

- +

Ćao, {firstName} {lastName}

+
); } @@ -1274,7 +1274,7 @@ h1 { margin-top: 10px; } -First, import `useState` from React. Then replace `firstName` and `lastName` with state variables declared by calling `useState`. Finally, replace every `firstName = ...` assignment with `setFirstName(...)`, and do the same for `lastName`. Don't forget to update `handleReset` too so that the reset button works. +Prvo, import-ujte `useState` iz React-a. Onda zamenite `firstName` i `lastName` sa state promenljivama deklarisanim pozivanjem `useState`. Na kraju, zamenite svaku `firstName = ...` dodelu sa `setFirstName(...)`, a onda uradite isto i za `lastName`. Ne zaboravite da promenite i `handleReset` kako bi dugme za resetovanje radilo. @@ -1301,17 +1301,17 @@ export default function Form() { return (
e.preventDefault()}> -

Hi, {firstName} {lastName}

- +

Ćao, {firstName} {lastName}

+
); } @@ -1325,13 +1325,13 @@ h1 { margin-top: 10px; }
-#### Fix a crash {/*fix-a-crash*/} +#### Popraviti problem {/*fix-a-crash*/} -Here is a small form that is supposed to let the user leave some feedback. When the feedback is submitted, it's supposed to display a thank-you message. However, it crashes with an error message saying "Rendered fewer hooks than expected". Can you spot the mistake and fix it? +Ovde je mala forma koja treba služiti korisnicima da unesu feedback. Kada se feedback submit-uje, trebalo bi da se prikaže poruka zahvalnosti. Međutim, skrši se sa porukom o grešci koja kaže "Renderovano manje hook-ova nego što je očekivano" ("Rendered fewer hooks than expected"). Možete li uočiti grešku i popraviti je? -Are there any limitations on _where_ Hooks may be called? Does this component break any rules? Check if there are any comments disabling the linter checks--this is where the bugs often hide! +Postoje li ograničenja _gde_ se Hook-ovi mogu pozvati? Da li komponenta krši neka pravila? Proverite da li postoje neki komentari koji gase linter provere--to je mesto gde se bug-ovi često kriju! @@ -1343,23 +1343,23 @@ import { useState } from 'react'; export default function FeedbackForm() { const [isSent, setIsSent] = useState(false); if (isSent) { - return

Thank you!

; + return

Hvala!

; } else { // eslint-disable-next-line const [message, setMessage] = useState(''); return (
{ e.preventDefault(); - alert(`Sending: "${message}"`); + alert(`Slanje: "${message}"`); setIsSent(true); }}>