Skip to content

Commit b828c75

Browse files
authored
Merge pull request #104 from lxmarinkovic/adding-interactivity
Adding interactivity
2 parents 16c1ab4 + 407fd33 commit b828c75

File tree

1 file changed

+49
-50
lines changed

1 file changed

+49
-50
lines changed

src/content/learn/adding-interactivity.md

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
---
2-
title: Adding Interactivity
2+
title: Dodavanje interaktivnosti
33
---
44

55
<Intro>
66

7-
Some things on the screen update in response to user input. For example, clicking an image gallery switches the active image. In React, data that changes over time is called *state.* You can add state to any component, and update it as needed. In this chapter, you'll learn how to write components that handle interactions, update their state, and display different output over time.
7+
Neke stvari na ekranu se update-uju kao odgovor na korisnički unos. Na primer, klik na galeriju slika menja aktivnu sliku. U React-u, podaci koji se menjaju tokom vremena nazivaju se *state.* Možete dodati state bilo kojoj component-i i update-ovati je po potrebi. U ovom poglavlju naučićete kako da pišete component-e koji obrađuju interakcije, update-uju svoj state i prikazuju različite output-e tokom vremena.
88

99
</Intro>
1010

1111
<YouWillLearn isChapter={true}>
1212

13-
* [How to handle user-initiated events](/learn/responding-to-events)
14-
* [How to make components "remember" information with state](/learn/state-a-components-memory)
15-
* [How React updates the UI in two phases](/learn/render-and-commit)
16-
* [Why state doesn't update right after you change it](/learn/state-as-a-snapshot)
17-
* [How to queue multiple state updates](/learn/queueing-a-series-of-state-updates)
18-
* [How to update an object in state](/learn/updating-objects-in-state)
19-
* [How to update an array in state](/learn/updating-arrays-in-state)
13+
* [Kako da rukujette event-ima koje pokrene korisnik](/learn/responding-to-events)
14+
* [Kako da učinite da component-e „pamte” informacije koristeći state](/learn/state-a-components-memory)
15+
* [Kako React update-uje UI u dve faze](/learn/render-and-commit)
16+
* [Zašto se state ne update-uje odmah nakon promene](/learn/state-as-a-snapshot)
17+
* [Kako da složite seriju state update-ova](/learn/queueing-a-series-of-state-updates)
18+
* [Kako da update-ujete objekat u state-u](/learn/updating-objects-in-state)
19+
* [Kako da update-ujete array u state-u](/learn/updating-arrays-in-state)
2020

2121
</YouWillLearn>
2222

23-
## Responding to events {/*responding-to-events*/}
23+
## Odgovaranje na event-e {/*responding-to-events*/}
2424

25-
React lets you add *event handlers* to your JSX. Event handlers are your own functions that will be triggered in response to user interactions like clicking, hovering, focusing on form inputs, and so on.
25+
React vam omogućava da dodate *event handler*-e u vaš JSX. Event handler-i su vaše sopstvene funkcije koje će se pokrenuti kao odgovor na korisničke interakcije, poput klika, prelaženja mišem, fokusiranja na form input-e i tako dalje.
2626

27-
Built-in components like `<button>` only support built-in browser events like `onClick`. However, you can also create your own components, and give their event handler props any application-specific names that you like.
27+
Ugrađene component-e poput `<button>` podržavaju samo ugrađene browser event-e poput `onClick`. Međutim, možete kreirati i sopstvene component-e i njihovim event handler prop-ovima dati bilo koja aplikacijski specifična imena koja želite.
2828

2929
<Sandpack>
3030

@@ -68,15 +68,15 @@ button { margin-right: 10px; }
6868

6969
<LearnMore path="/learn/responding-to-events">
7070

71-
Read **[Responding to Events](/learn/responding-to-events)** to learn how to add event handlers.
71+
Pročitajte **[Odgovaranje na Event-e](/learn/responding-to-events)** da biste naučili kako da dodate event handler-e.
7272

7373
</LearnMore>
7474

75-
## State: a component's memory {/*state-a-components-memory*/}
75+
## State: memorija component-e {/*state-a-components-memory*/}
7676

77-
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" puts 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.*
77+
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.*
7878

79-
You can add state to a component with a [`useState`](/reference/react/useState) Hook. *Hooks* are special functions that let your components use React features (state is one of those features). The `useState` Hook lets you declare a state variable. It takes the initial state and returns a pair of values: the current state, and a state setter function that lets you update it.
79+
Možete dodati state component-i pomoću Hook-a [`useState`](/reference/react/useState). *Hook-ovi* su specijalne funkcije koje omogućavaju vašim component-ama da koriste React funkcionalnosti (state je jedna od tih funkcionalnosti). Hook `useState` vam omogućava da deklarišete varijablu state-a. On prima inicijalni state i vraća par vrednosti: trenutni state i funkciju za setovanje state-a koja vam omogućava da ga update-ujete.
8080

8181
```js
8282
const [index, setIndex] = useState(0);
@@ -229,43 +229,43 @@ button {
229229

230230
<LearnMore path="/learn/state-a-components-memory">
231231

232-
Read **[State: A Component's Memory](/learn/state-a-components-memory)** to learn how to remember a value and update it on interaction.
232+
Pročitajte **[State: Memorija Component-e](/learn/state-a-components-memory)** kako biste naučili kako da zapamtite vrednost i update-ujete je prilikom interakcije.
233233

234234
</LearnMore>
235235

236-
## Render and commit {/*render-and-commit*/}
236+
## Render i commit {/*render-and-commit*/}
237237

238-
Before your components are displayed on the screen, they must be rendered by React. Understanding the steps in this process will help you think about how your code executes and explain its behavior.
238+
Pre nego što se vaše component-e prikažu na ekranu, React mora da ih render-uje. Razumevanje koraka u ovom procesu pomoći će vam da razmislite o tome kako se vaš kod izvršava i da objasnite njegovo ponašanje.
239239

240-
Imagine that your components are cooks in the kitchen, assembling tasty dishes from ingredients. In this scenario, React is the waiter who puts in requests from customers and brings them their orders. This process of requesting and serving UI has three steps:
240+
Zamislite da su vaše component-e kuvari u kuhinji, koji pripremaju ukusna jela od sastojaka. U ovom scenariju, React je konobar koji prenosi narudžbine od gostiju i donosi im njihova jela. Ovaj proces naručivanja i posluživanja UI-a ima tri koraka:
241241

242-
1. **Triggering** a render (delivering the diner's order to the kitchen)
243-
2. **Rendering** the component (preparing the order in the kitchen)
244-
3. **Committing** to the DOM (placing the order on the table)
242+
1. **Pokretanje** render-a (prenos narudžbine gosta u kuhinju)
243+
2. **Render-ovanje** component-e (priprema narudžbine u kuhinji)
244+
3. **Commit-ovanje** u DOM (postavljanje narudžbine na sto)
245245

246246
<IllustrationBlock sequential>
247-
<Illustration caption="Trigger" alt="React as a server in a restaurant, fetching orders from the users and delivering them to the Component Kitchen." src="/images/docs/illustrations/i_render-and-commit1.png" />
248-
<Illustration caption="Render" alt="The Card Chef gives React a fresh Card component." src="/images/docs/illustrations/i_render-and-commit2.png" />
249-
<Illustration caption="Commit" alt="React delivers the Card to the user at their table." src="/images/docs/illustrations/i_render-and-commit3.png" />
247+
<Illustration caption="Trigger" alt="React kao konobar u restoranu, prenosi narudžbine od korisnika i dostavlja ih u Component-u Kitchen." src="/images/docs/illustrations/i_render-and-commit1.png" />
248+
<Illustration caption="Render" alt="Šef kuhinje za Card daje React-u svežu Card component-u." src="/images/docs/illustrations/i_render-and-commit2.png" />
249+
<Illustration caption="Commit" alt="React donosi Card korisniku za njihov sto." src="/images/docs/illustrations/i_render-and-commit3.png" />
250250
</IllustrationBlock>
251251

252252
<LearnMore path="/learn/render-and-commit">
253253

254-
Read **[Render and Commit](/learn/render-and-commit)** to learn the lifecycle of a UI update.
254+
Pročitajte **[Render i Commit](/learn/render-and-commit)** kako biste naučili lifecycle jednog UI update-a.
255255

256256
</LearnMore>
257257

258-
## State as a snapshot {/*state-as-a-snapshot*/}
258+
## State kao snapshot {/*state-as-a-snapshot*/}
259259

260-
Unlike regular JavaScript variables, React state behaves more like a snapshot. Setting it does not change the state variable you already have, but instead triggers a re-render. This can be surprising at first!
260+
Za razliku od običnih JavaScript varijabli, React state se ponaša više kao snapshot. Njegovo postavljanje ne menja već postojeću state varijablu, već umesto toga pokreće re-render. Ovo može biti iznenađujuće na početku!
261261

262262
```js
263263
console.log(count); // 0
264264
setCount(count + 1); // Request a re-render with 1
265265
console.log(count); // Still 0!
266266
```
267267

268-
This behavior helps you avoid subtle bugs. Here is a little chat app. Try to guess what happens if you press "Send" first and *then* change the recipient to Bob. Whose name will appear in the `alert` five seconds later?
268+
Ovo ponašanje vam pomaže da izbegnete suptilne greške. Evo male aplikacije za ćaskanje. Pokušajte da pogodite šta će se desiti ako prvo pritisnete "Pošalji", a *zatim* promenite primaoca na Bob. Čije ime će se pojaviti u `alert` pet sekundi kasnije?
269269

270270
<Sandpack>
271271

@@ -279,7 +279,7 @@ export default function Form() {
279279
function handleSubmit(e) {
280280
e.preventDefault();
281281
setTimeout(() => {
282-
alert(`You said ${message} to ${to}`);
282+
alert(`Poruka ${message} za ${to}`);
283283
}, 5000);
284284
}
285285

@@ -311,16 +311,15 @@ label, textarea { margin-bottom: 10px; display: block; }
311311

312312
</Sandpack>
313313

314-
315314
<LearnMore path="/learn/state-as-a-snapshot">
316315

317-
Read **[State as a Snapshot](/learn/state-as-a-snapshot)** to learn why state appears "fixed" and unchanging inside the event handlers.
316+
Pročitajte **[State kao Snapshot](/learn/state-as-a-snapshot)** da biste saznali zašto state izgleda "fiksno" i nepromenljivo unutar event handler-a.
318317

319318
</LearnMore>
320319

321-
## Queueing a series of state updates {/*queueing-a-series-of-state-updates*/}
320+
## Redosled serijskih update-ova state-a {/*queueing-a-series-of-state-updates*/}
322321

323-
This component is buggy: clicking "+3" increments the score only once.
322+
Ova component-a ima grešku: klikom na dugme "+3" rezultat se povećava samo jednom.
324323

325324
<Sandpack>
326325

@@ -354,7 +353,7 @@ button { display: inline-block; margin: 10px; font-size: 20px; }
354353

355354
</Sandpack>
356355

357-
[State as a Snapshot](/learn/state-as-a-snapshot) explains why this is happening. Setting state requests a new re-render, but does not change it in the already running code. So `score` continues to be `0` right after you call `setScore(score + 1)`.
356+
[State kao Snapshot](/learn/state-as-a-snapshot) objašnjava zašto se ovo dešava. Postavljanje state-a traži novo render-ovanje, ali ne menja njegovu vrednost u kodu koji se već izvršava. Tako `score` nastavlja da ima vrednost `0` odmah nakon što pozovete `setScore(score + 1)`.
358357

359358
```js
360359
console.log(score); // 0
@@ -366,7 +365,7 @@ setScore(score + 1); // setScore(0 + 1);
366365
console.log(score); // 0
367366
```
368367

369-
You can fix this by passing an *updater function* when setting state. Notice how replacing `setScore(score + 1)` with `setScore(s => s + 1)` fixes the "+3" button. This lets you queue multiple state updates.
368+
Možete ovo popraviti prosleđivanjem *updater funkcije* kada postavljate state. Obratite pažnju kako zamena `setScore(score + 1)` sa `setScore(s => s + 1)` rešava problem dugmeta "+3". Ovo vam omogućava da redom dodate više update-ova state-a u redosled.
370369

371370
<Sandpack>
372371

@@ -402,15 +401,15 @@ button { display: inline-block; margin: 10px; font-size: 20px; }
402401

403402
<LearnMore path="/learn/queueing-a-series-of-state-updates">
404403

405-
Read **[Queueing a Series of State Updates](/learn/queueing-a-series-of-state-updates)** to learn how to queue a sequence of state updates.
404+
Pročitajte **Redosled serije state update-ova](/learn/queueing-a-series-of-state-updates)** kako biste naučili kako da postavite redosled update-ova state-a.
406405

407406
</LearnMore>
408407

409-
## Updating objects in state {/*updating-objects-in-state*/}
408+
## Update-ovanje objekata u state-u {/*updating-objects-in-state*/}
410409

411-
State can hold any kind of JavaScript value, including objects. But you shouldn't change objects and arrays that you hold in the React state directly. Instead, when you want to update an object and array, you need to create a new one (or make a copy of an existing one), and then update the state to use that copy.
410+
State može sadržati bilo koju vrstu JavaScript vrednosti, uključujući objekte. Međutim, ne bi trebalo direktno menjati objekte i array-e koje držite u React state-u. Umesto toga, kada želite da update-ujete objekat ili array, potrebno je da kreirate novi (ili napravite kopiju postojećeg) i zatim update-ujete state kako bi koristio tu kopiju.
412411

413-
Usually, you will use the `...` spread syntax to copy objects and arrays that you want to change. For example, updating a nested object could look like this:
412+
Obično ćete koristiti `...` spread sintaksu za kopiranje objekata i array-a koje želite da promenite. Na primer, update-ovanje ugnježdenog objekta može izgledati ovako:
414413

415414
<Sandpack>
416415

@@ -518,7 +517,7 @@ img { width: 200px; height: 200px; }
518517

519518
</Sandpack>
520519

521-
If copying objects in code gets tedious, you can use a library like [Immer](https://github.com/immerjs/use-immer) to reduce repetitive code:
520+
Ako kopiranje objekata u kodu postane zamorno, možete koristiti biblioteku poput [Immer](https://github.com/immerjs/use-immer) kako biste smanjili količinu ponavljajućeg koda:
522521

523522
<Sandpack>
524523

@@ -633,13 +632,13 @@ img { width: 200px; height: 200px; }
633632

634633
<LearnMore path="/learn/updating-objects-in-state">
635634

636-
Read **[Updating Objects in State](/learn/updating-objects-in-state)** to learn how to update objects correctly.
635+
Pročitajte **[Update-ovanje Objekata u State-u](/learn/updating-objects-in-state)** kako biste naučili kako pravilno update-ovati objekte.
637636

638637
</LearnMore>
639638

640-
## Updating arrays in state {/*updating-arrays-in-state*/}
639+
## Update-ovanje array-a u state-u {/*updating-arrays-in-state*/}
641640

642-
Arrays are another type of mutable JavaScript objects you can store in state and should treat as read-only. Just like with objects, when you want to update an array stored in state, you need to create a new one (or make a copy of an existing one), and then set state to use the new array:
641+
Array-i su još jedan tip promenljivih JavaScript objekata koje možete čuvati u state-u i koje treba tretirati kao read-only. Kao i kod objekata, kada želite da update-ujete array koji se nalazi u state-u, potrebno je da kreirate novi (ili napravite kopiju postojećeg), a zatim postavite state da koristi taj novi array:
643642

644643
<Sandpack>
645644

@@ -705,7 +704,7 @@ function ItemList({ artworks, onToggle }) {
705704

706705
</Sandpack>
707706

708-
If copying arrays in code gets tedious, you can use a library like [Immer](https://github.com/immerjs/use-immer) to reduce repetitive code:
707+
Ako pravljenje kopija array-a u kodu postane zamorno, možete koristiti biblioteku kao što je [Immer](https://github.com/immerjs/use-immer) da smanjite ponavljanje koda:
709708

710709
<Sandpack>
711710

@@ -789,12 +788,12 @@ function ItemList({ artworks, onToggle }) {
789788

790789
<LearnMore path="/learn/updating-arrays-in-state">
791790

792-
Read **[Updating Arrays in State](/learn/updating-arrays-in-state)** to learn how to update arrays correctly.
791+
Pročitajte **[Update-ovanje Array-a u State-u](/learn/updating-arrays-in-state)** da biste naučili kako pravilno da update-ujete array-e.
793792

794793
</LearnMore>
795794

796-
## What's next? {/*whats-next*/}
795+
## Šta je sledeće? {/*whats-next*/}
797796

798-
Head over to [Responding to Events](/learn/responding-to-events) to start reading this chapter page by page!
797+
Pređite na [Reagovanje na Event-e](/learn/responding-to-events) da biste počeli da čitate ovo poglavlje stranicu po stranicu!
799798

800-
Or, if you're already familiar with these topics, why not read about [Managing State](/learn/managing-state)?
799+
Ili, ako ste već upoznati sa ovim temama, zašto ne biste pročitali [Upravljanje State-om](/learn/managing-state)?

0 commit comments

Comments
 (0)