diff --git a/src/content/learn/describing-the-ui.md b/src/content/learn/describing-the-ui.md index b41200b5..21608f70 100644 --- a/src/content/learn/describing-the-ui.md +++ b/src/content/learn/describing-the-ui.md @@ -525,16 +525,16 @@ Pročitajte **[Održavanje komponenata čistim](/learn/keeping-components-pure)* -## Razumevanje vašeg UI kao stabla {/*understanding-your-ui-as-a-tree*/} +## Razumevanje vašeg UI-a kao stabla {/*understanding-your-ui-as-a-tree*/} React koristi stabla da modeluje odnose između komponenata i modula. -React render stablo je reprezentacija odnosa roditelj-dete između komponenata. +React stablo renderovanja je reprezentacija odnosa roditelj-dete između komponenata. -Primer render stabla. +Primer stabla renderovanja. diff --git a/src/content/learn/managing-state.md b/src/content/learn/managing-state.md index 555bf922..83c0f9b7 100644 --- a/src/content/learn/managing-state.md +++ b/src/content/learn/managing-state.md @@ -399,7 +399,7 @@ textarea { -React vam omogućava da override-ujete default ponašanje i *forsirate* komponentu da resetuje svoje stanje prosleđivanjem različitog `key`-a, na primer ``. Ovo govori React-u da ako je primalac drugačiji, treba da smatra da se *drugačija* `Chat` komponenta treba ponovo kreirati od nule sa novim podacima (i UI-jem poput input-a). Sada, promena primaoca resetuje polje za input--iako renderujete istu komponentu. +React vam omogućava da override-ujete default ponašanje i *forsirate* komponentu da resetuje svoj state prosleđivanjem različitog `key`-a, na primer ``. Ovo govori React-u da ako je primalac drugačiji, treba da smatra da se *drugačija* `Chat` komponenta treba ponovo kreirati od nule sa novim podacima (i UI-jem poput input-a). Sada, promena primaoca resetuje polje za input--iako renderujete istu komponentu. diff --git a/src/content/learn/preserving-and-resetting-state.md b/src/content/learn/preserving-and-resetting-state.md index 11d398d2..0b908d34 100644 --- a/src/content/learn/preserving-and-resetting-state.md +++ b/src/content/learn/preserving-and-resetting-state.md @@ -1,28 +1,28 @@ --- -title: Preserving and Resetting State +title: Čuvanje i resetovanje state-a --- -State is isolated between components. React keeps track of which state belongs to which component based on their place in the UI tree. You can control when to preserve state and when to reset it between re-renders. +State je izolovan između komponenata. React prati koji state pripada kojoj komponenti na osnovu njenog mesta u UI stablu. Možete kontrolisati kada čuvati state, a kada ga resetovati između ponovnih rendera. -* When React chooses to preserve or reset the state -* How to force React to reset component's state -* How keys and types affect whether the state is preserved +* Kada React bira da li da čuva ili resetuje state +* Kako naterati React da resetuje state komponente +* Kako ključevi i tipovi utiču na to da li je state sačuvan -## State is tied to a position in the render tree {/*state-is-tied-to-a-position-in-the-tree*/} +## State je povezan sa pozicijom u stablu renderovanja {/*state-is-tied-to-a-position-in-the-tree*/} -React builds [render trees](learn/understanding-your-ui-as-a-tree#the-render-tree) for the component structure in your UI. +React pravi [stabla renderovanja](learn/understanding-your-ui-as-a-tree#the-render-tree) za strukturu komponenti na vašem UI-u. -When you give a component state, you might think the state "lives" inside the component. But the state is actually held inside React. React associates each piece of state it's holding with the correct component by where that component sits in the render tree. +Kada komponenti date state, možete pomisliti da state "živi" unutar komponente. Ali, state se zapravo čuva unutar React-a. React povezuje svaki deo state-a sa tačnom komponentom na osnovu mesta na kom se ta komponenta nalazi u stablu renderovanja. -Here, there is only one `` JSX tag, but it's rendered at two different positions: +Ovde postoji samo jedan `` JSX tag, ali je renderovan na dve različite pozicije: @@ -56,7 +56,7 @@ function Counter() { >

{score}

); @@ -86,23 +86,23 @@ label {
-Here's how these look as a tree: +Evo kako to izgleda u obliku stabla: - + -React tree +React stablo -**These are two separate counters because each is rendered at its own position in the tree.** You don't usually have to think about these positions to use React, but it can be useful to understand how it works. +**Ovo su dva odvojena brojača jer je svaki renderovan na svojoj poziciji u stablu.** Obično ne morate razmišljati o ovim pozicijama da biste koristili React, ali može biti korisno da razumete kako to funkcioniše. -In React, each component on the screen has fully isolated state. For example, if you render two `Counter` components side by side, each of them will get its own, independent, `score` and `hover` states. +U React-u, svaka komponenta na ekranu ima potpuno izolovan state. Na primer, ako renderujete dve `Counter` komponente jednu pored druge, svaka će imati svoje nezavisne `score` i `hover` state-ove. -Try clicking both counters and notice they don't affect each other: +Probajte da kliknete oba brojača i primetite da ne utiču jedan na drugog: @@ -135,7 +135,7 @@ function Counter() { >

{score}

); @@ -160,21 +160,21 @@ function Counter() {
-As you can see, when one counter is updated, only the state for that component is updated: +Kao što možete videti, kada je jedan brojač ažuriran, jedino se state za tu komponentu ažurira: - + -Updating state +Ažuriranje state-a -React will keep the state around for as long as you render the same component at the same position in the tree. To see this, increment both counters, then remove the second component by unchecking "Render the second counter" checkbox, and then add it back by ticking it again: +React će držati state dok god istu komponentu renderujete na istoj poziciji u stablu. Da biste ovo primetili, inkrementirajte oba brojača, uklonite drugu komponentu tako što ćete odštiklirati checkbox "Renderuj drugi brojač", a onda je štikliranjem dodajte ponovo: @@ -195,7 +195,7 @@ export default function App() { setShowB(e.target.checked) }} /> - Render the second counter + Renderuj drugi brojač ); @@ -218,7 +218,7 @@ function Counter() { >

{score}

); @@ -248,35 +248,35 @@ label {
-Notice how the moment you stop rendering the second counter, its state disappears completely. That's because when React removes a component, it destroys its state. +Primetite da u trenutku kada prestanete da renderujete drugi brojač, njegov state potpuno nestaje. To je zato što kad React ukloni komponentu, uklanja i njen state. - + -Deleting a component +Brisanje komponente -When you tick "Render the second counter", a second `Counter` and its state are initialized from scratch (`score = 0`) and added to the DOM. +Kada štiklirate "Renderuj drugi brojač", drugi `Counter` i njegov state se inicijalizuju od nule (`score = 0`) i dodaju u DOM. - + -Adding a component +Dodavanje komponente -**React preserves a component's state for as long as it's being rendered at its position in the UI tree.** If it gets removed, or a different component gets rendered at the same position, React discards its state. +**React čuva state komponente dok god se renderuje na svojoj poziciji u UI stablu.** Ako se ukloni, ili se druga komponenta renderuje na istoj poziciji, React odbacuje njen state. -## Same component at the same position preserves state {/*same-component-at-the-same-position-preserves-state*/} +## Ista komponenta na istoj poziciji čuva state {/*same-component-at-the-same-position-preserves-state*/} -In this example, there are two different `` tags: +U ovom primeru postoje dva različita `` tag-a: @@ -300,7 +300,7 @@ export default function App() { setIsFancy(e.target.checked) }} /> - Use fancy styling + Koristi fensi stajling ); @@ -326,7 +326,7 @@ function Counter({ isFancy }) { >

{score}

); @@ -361,24 +361,24 @@ label {
-When you tick or clear the checkbox, the counter state does not get reset. Whether `isFancy` is `true` or `false`, you always have a `` as the first child of the `div` returned from the root `App` component: +Kada promenite checkbox, counter state ne bude resetovan. Nezavisno od toga da li je `isFancy` jednak `true` ili `false`, uvek ćete imati `` kao prvo dete `div`-a koje je vraćeno iz korenske `App` komponente: - + -Updating the `App` state does not reset the `Counter` because `Counter` stays in the same position +Ažuriranje `App` state-a ne resetuje `Counter` zato što `Counter` ostaje na istoj poziciji -It's the same component at the same position, so from React's perspective, it's the same counter. +To je ista komponenta na istoj poziciji, pa je iz React perspektive to isti brojač. -Remember that **it's the position in the UI tree--not in the JSX markup--that matters to React!** This component has two `return` clauses with different `` JSX tags inside and outside the `if`: +Zapamtite da je **pozicija u UI stablu--ne u JSX markup-u--ono što je React-u bitno**! Ova komponenta ima dva `return` iskaza sa različitim `` JSX tag-ovima unutar i izvan `if`-a: @@ -399,7 +399,7 @@ export default function App() { setIsFancy(e.target.checked) }} /> - Use fancy styling + Koristi fensi stajling ); @@ -415,7 +415,7 @@ export default function App() { setIsFancy(e.target.checked) }} /> - Use fancy styling + Koristi fensi stajling ); @@ -441,7 +441,7 @@ function Counter({ isFancy }) { >

{score}

); @@ -476,15 +476,15 @@ label {
-You might expect the state to reset when you tick checkbox, but it doesn't! This is because **both of these `` tags are rendered at the same position.** React doesn't know where you place the conditions in your function. All it "sees" is the tree you return. +Možda biste očekivali da se state resetuje kada promenite checkbox, ali to nije slučaj! To se dešava jer su **oba `` tag-a renderovana na istoj poziciji**. React ne zna gde vi pravite uslove u vašoj funkciji. Sve što "vidi" je stablo koje vratite. -In both cases, the `App` component returns a `
` with `` as a first child. To React, these two counters have the same "address": the first child of the first child of the root. This is how React matches them up between the previous and next renders, regardless of how you structure your logic. +U oba slučaja, `App` komponenta vraća `
` sa `` kao prvim detetom. Za React, ova dva brojača imaju iste "adrese": prvo dete prvog deteta od korena. Ovako ih React poredi između prethodnih i narednih rendera, nezavisno od strukture vaše logike. -## Different components at the same position reset state {/*different-components-at-the-same-position-reset-state*/} +## Različite komponente na istoj poziciji resetuju state {/*different-components-at-the-same-position-reset-state*/} -In this example, ticking the checkbox will replace `` with a `

`: +U ovom primeru, štikliranje checkbox-a će zameniti `` sa `

`: @@ -496,7 +496,7 @@ export default function App() { return (

{isPaused ? ( -

See you later!

+

Vidimo se posle!

) : ( )} @@ -508,7 +508,7 @@ export default function App() { setIsPaused(e.target.checked) }} /> - Take a break + Uzmi pauzu
); @@ -531,7 +531,7 @@ function Counter() { >

{score}

); @@ -561,13 +561,13 @@ label { -Here, you switch between _different_ component types at the same position. Initially, the first child of the `
` contained a `Counter`. But when you swapped in a `p`, React removed the `Counter` from the UI tree and destroyed its state. +Ovde menjate između _različitih_ tipova komponenti na istoj poziciji. Inicijalno, prvo dete `
`-a je bio `Counter`. Ali kad ste ga zamenili sa `p`, React je uklonio `Counter` iz UI stabla i uništio njegov state. - + -When `Counter` changes to `p`, the `Counter` is deleted and the `p` is added +Kada se `Counter` zameni sa `p`, `Counter` je obrisan i `p` je dodat @@ -575,15 +575,15 @@ When `Counter` changes to `p`, the `Counter` is deleted and the `p` is added - + -When switching back, the `p` is deleted and the `Counter` is added +Kada se ponovo promene, `p` je obrisan, a `Counter` je dodat -Also, **when you render a different component in the same position, it resets the state of its entire subtree.** To see how this works, increment the counter and then tick the checkbox: +Takođe, **kada renderujete različitu komponentu na istoj poziciji, resetuje se state od čitavog podstabla**. Da vidite kako ovo radi, inkrementirajte brojač i onda štiklirajte checkbox: @@ -611,7 +611,7 @@ export default function App() { setIsFancy(e.target.checked) }} /> - Use fancy styling + Koristi fensi stajling
); @@ -637,7 +637,7 @@ function Counter({ isFancy }) { >

{score}

); @@ -672,13 +672,13 @@ label { -The counter state gets reset when you click the checkbox. Although you render a `Counter`, the first child of the `div` changes from a `section` to a `div`. When the child `section` was removed from the DOM, the whole tree below it (including the `Counter` and its state) was destroyed as well. +State `Counter`-a se resetuje kada kliknete na checkbox. Iako renderujete `Counter`, prvo dete `div`-a se promeni sa `section` na `div`. Kada je dečji `section` uklonjen iz DOM-a, celokupno stablo ispod (uključujući `Counter` i njegov state) je takođe uništeno. - + -When `section` changes to `div`, the `section` is deleted and the new `div` is added +Kada se `section` promeni u `div`, `section` je obrisan, a novi `div` je dodat @@ -686,21 +686,21 @@ When `section` changes to `div`, the `section` is deleted and the new `div` is a - + -When switching back, the `div` is deleted and the new `section` is added +Kada se ponovo promene, `div` je obrisan, a novi `section` je dodat -As a rule of thumb, **if you want to preserve the state between re-renders, the structure of your tree needs to "match up"** from one render to another. If the structure is different, the state gets destroyed because React destroys state when it removes a component from the tree. +Kao pravilo, **ako želite da sačuvate state između ponovnih rendera, struktura vašeg stabla mora da se "poklapa"** od jednog do drugog rendera. Ako je struktura različita, state će biti uništen jer React uništava state kada uklanja komponentu iz stabla. -This is why you should not nest component function definitions. +Zbog ovoga ne biste trebali da ugnježdavate definicije funkcija komponenti. -Here, the `MyTextField` component function is defined *inside* `MyComponent`: +Ovde je funkcija `MyTextField` komponente definisana *unutar* `MyComponent`: @@ -726,7 +726,7 @@ export default function MyComponent() { + }}>Kliknuto {counter} puta ); } @@ -735,13 +735,13 @@ export default function MyComponent() { -Every time you click the button, the input state disappears! This is because a *different* `MyTextField` function is created for every render of `MyComponent`. You're rendering a *different* component in the same position, so React resets all state below. This leads to bugs and performance problems. To avoid this problem, **always declare component functions at the top level, and don't nest their definitions.** +Svaki put kad kliknete dugme, input state nestane! To se dešava jer je *različita* `MyTextField` funkcija kreirana za svaki render `MyComponent`-a. Renderujete *različitu* komponentu na istoj poziciji, pa React resetuje sve state-ove ispod. Ovo prouzrokuje bug-ove i probleme sa performansama. Da biste izbegli ovaj problem, **uvek deklarišite funkcije komponenti na najvišem nivou i nemojte ugnježdavati njihove definicije**. -## Resetting state at the same position {/*resetting-state-at-the-same-position*/} +## Resetovanje state-a na istoj poziciji {/*resetting-state-at-the-same-position*/} -By default, React preserves state of a component while it stays at the same position. Usually, this is exactly what you want, so it makes sense as the default behavior. But sometimes, you may want to reset a component's state. Consider this app that lets two players keep track of their scores during each turn: +Po default-u, React čuva state komponente dok god ostaje na istoj poziciji. Uglavnom je to upravo ono što želite, pa ima smisla da je to default ponašanje. Ali, ponekad možete želeti da resetujete state komponente. Razmotrite ovu aplikaciju koja omogućava dvojici igrača da prate svoje poene tokom svakog poteza: @@ -760,7 +760,7 @@ export default function Scoreboard() {
); @@ -781,9 +781,9 @@ function Counter({ person }) { onPointerEnter={() => setHover(true)} onPointerLeave={() => setHover(false)} > -

{person}'s score: {score}

+

Poeni za osobu {person}: {score}

); @@ -811,19 +811,19 @@ h1 {
-Currently, when you change the player, the score is preserved. The two `Counter`s appear in the same position, so React sees them as *the same* `Counter` whose `person` prop has changed. +Trenutno, kada promenite igrača, poeni ostaju sačuvani. Dve `Counter` komponente se nalaze na istoj poziciji, pa ih React posmatra kao *isti* `Counter` čiji se `person` prop promenio. -But conceptually, in this app they should be two separate counters. They might appear in the same place in the UI, but one is a counter for Taylor, and another is a counter for Sarah. +Ali konceptualno, u ovoj aplikaciji to trebaju biti dva odvojena brojača. Iako se pojavljuju na istom mestu na UI-u, jedan brojač je za Taylor, a drugi za Sarah. -There are two ways to reset state when switching between them: +Postoje dva načina da resetujete state kada ih menjate: -1. Render components in different positions -2. Give each component an explicit identity with `key` +1. Renderujte komponente na različitim pozicijama +2. Dajte svakoj komponenti eksplicitni identitet sa `key` -### Option 1: Rendering a component in different positions {/*option-1-rendering-a-component-in-different-positions*/} +### Opcija 1: Renderovanje komponente na različitim pozicijama {/*option-1-rendering-a-component-in-different-positions*/} -If you want these two `Counter`s to be independent, you can render them in two different positions: +Ako želite da ova dva `Counter`-a budu nezavisna, možete ih renderovati na dve različite pozicije: @@ -843,7 +843,7 @@ export default function Scoreboard() { ); @@ -864,9 +864,9 @@ function Counter({ person }) { onPointerEnter={() => setHover(true)} onPointerLeave={() => setHover(false)} > -

{person}'s score: {score}

+

Poeni za osobu {person}: {score}

); @@ -894,42 +894,42 @@ h1 {
-* Initially, `isPlayerA` is `true`. So the first position contains `Counter` state, and the second one is empty. -* When you click the "Next player" button the first position clears but the second one now contains a `Counter`. +* Inicijalno, `isPlayerA` je `true`. To znači da prva pozicija sadrži `Counter` state, a druga je prazna. +* Kada kliknete dugme "Naredni igrač" prva pozicija se briše, a druga sada sadrži `Counter`. - + -Initial state +Inicijalni state - + -Clicking "next" +Kliknuto "naredno" - + -Clicking "next" again +Kliknuto "naredno" opet -Each `Counter`'s state gets destroyed each time it's removed from the DOM. This is why they reset every time you click the button. +State svakog `Counter`-a bude uništen svaki put kad je uklonjen iz DOM-a. Zbog toga se resetuju svaki put kada kliknete dugme. -This solution is convenient when you only have a few independent components rendered in the same place. In this example, you only have two, so it's not a hassle to render both separately in the JSX. +Ovo rešenje je zgodno kada imate samo par komponenti renderovanih na istom mestu. U ovom primeru, imate ih samo dve, pa nije problem renderovati ih u različitom JSX-u. -### Option 2: Resetting state with a key {/*option-2-resetting-state-with-a-key*/} +### Opcija 2: Resetovanje state-a uz key {/*option-2-resetting-state-with-a-key*/} -There is also another, more generic, way to reset a component's state. +Postoji i drugi, više generički, način da resetujete state komponente. -You might have seen `key`s when [rendering lists.](/learn/rendering-lists#keeping-list-items-in-order-with-key) Keys aren't just for lists! You can use keys to make React distinguish between any components. By default, React uses order within the parent ("first counter", "second counter") to discern between components. But keys let you tell React that this is not just a *first* counter, or a *second* counter, but a specific counter--for example, *Taylor's* counter. This way, React will know *Taylor's* counter wherever it appears in the tree! +Možda ste videli `key`-eve tokom [renderovanja listi](/learn/rendering-lists#keeping-list-items-in-order-with-key). Ključevi nisu samo za liste! Možete koristiti ključeve da naterate React da razlikuje bilo koje komponente. Po default-u, React koristi redosled unutar roditelja ("prvi brojač", "drugi brojač") da razlikuje komponente. Ali, ključevi vam omogućavaju da kažete React-u da to nije samo *prvi* brojač, ili *drugi* brojač, već poseban brojač--na primer, *Taylor-ov* brojač. Na ovaj način, React će znati za *Taylor-ov* brojač gde god da se pojavi u stablu! -In this example, the two ``s don't share state even though they appear in the same place in JSX: +U ovom primeru, dva ``-a ne dele state iako se nalaze na istom mestu u JSX-u: @@ -948,7 +948,7 @@ export default function Scoreboard() { ); @@ -969,9 +969,9 @@ function Counter({ person }) { onPointerEnter={() => setHover(true)} onPointerLeave={() => setHover(false)} > -

{person}'s score: {score}

+

Poeni za osobu {person}: {score}

); @@ -999,7 +999,7 @@ h1 {
-Switching between Taylor and Sarah does not preserve the state. This is because **you gave them different `key`s:** +Menjanje između Taylor i Sarah ne čuva state. To je zato što ste **im dali različite `key`-eve:** ```js {isPlayerA ? ( @@ -1009,19 +1009,19 @@ Switching between Taylor and Sarah does not preserve the state. This is because )} ``` -Specifying a `key` tells React to use the `key` itself as part of the position, instead of their order within the parent. This is why, even though you render them in the same place in JSX, React sees them as two different counters, and so they will never share state. Every time a counter appears on the screen, its state is created. Every time it is removed, its state is destroyed. Toggling between them resets their state over and over. +Specificiranje `key`-a govori React-u da koristi `key` kao deo pozicije umesto rasporeda unutar roditelja. Zbog toga, iako ih renderujete na istom mestu u JSX-u, React ih vidi kao dva različita brojača, tako da nikad neće deliti state. Svaki put kad se brojač pojavi na ekranu, njegov state je kreiran. Svaki put kad je uklonjen, njegov state je uništen. Promena između njih resetuje njihov state iznova. -Remember that keys are not globally unique. They only specify the position *within the parent*. +Zapamtite da ključevi nisu jedinstveni globalno. Oni samo označavaju poziciju *unutar roditelja*. -### Resetting a form with a key {/*resetting-a-form-with-a-key*/} +### Resetovanje forme sa key-em {/*resetting-a-form-with-a-key*/} -Resetting state with a key is particularly useful when dealing with forms. +Resetovanje state-a uz key je posebno korisno kada radite sa formama. -In this chat app, the `` component contains the text input state: +U ovoj aplikaciji za poruke, `` komponenta sadrži state za tekstualni input: @@ -1084,11 +1084,11 @@ export default function Chat({ contact }) {