From 44268351f16ecd8b5d28016277b7fa441f70db73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rado=C5=A1=20Mili=C4=87ev?= <40705899+rammba@users.noreply.github.com> Date: Thu, 25 Sep 2025 16:25:47 +0200 Subject: [PATCH] Translate useContext.md --- src/content/reference/react/useContext.md | 228 +++++++++++----------- 1 file changed, 114 insertions(+), 114 deletions(-) diff --git a/src/content/reference/react/useContext.md b/src/content/reference/react/useContext.md index f69c49af..b6ef43bf 100644 --- a/src/content/reference/react/useContext.md +++ b/src/content/reference/react/useContext.md @@ -4,7 +4,7 @@ title: useContext -`useContext` is a React Hook that lets you read and subscribe to [context](/learn/passing-data-deeply-with-context) from your component. +`useContext` je React Hook koji vam omogućava da čitate i da se pretplatite na [context](/learn/passing-data-deeply-with-context) iz vaše komponente. ```js const value = useContext(SomeContext) @@ -20,7 +20,7 @@ const value = useContext(SomeContext) ### `useContext(SomeContext)` {/*usecontext*/} -Call `useContext` at the top level of your component to read and subscribe to [context.](/learn/passing-data-deeply-with-context) +Pozovite `useContext` na vrhu vaše komponente da pročitate i da se pretplatite na [context](/learn/passing-data-deeply-with-context). ```js import { useContext } from 'react'; @@ -30,30 +30,30 @@ function MyComponent() { // ... ``` -[See more examples below.](#usage) +[Pogledajte još primera ispod.](#usage) -#### Parameters {/*parameters*/} +#### Parametri {/*parameters*/} -* `SomeContext`: The context that you've previously created with [`createContext`](/reference/react/createContext). The context itself does not hold the information, it only represents the kind of information you can provide or read from components. +* `SomeContext`: Context koji ste prethodno kreirali sa [`createContext`](/reference/react/createContext). Sam context ne sadrži informaciju, već samo predstavlja vrstu informacije koju možete pružiti i čitati iz komponenti. -#### Returns {/*returns*/} +#### Povratne vrednosti {/*returns*/} -`useContext` returns the context value for the calling component. It is determined as the `value` passed to the closest `SomeContext` above the calling component in the tree. If there is no such provider, then the returned value will be the `defaultValue` you have passed to [`createContext`](/reference/react/createContext) for that context. The returned value is always up-to-date. React automatically re-renders components that read some context if it changes. +`useContext` vraća vrednost context-a za pozivajuću komponentu. Određuje se kao `value` prosleđena najbližem `SomeContext`-u iznad pozivajuće komponente u stablu. Ako ne postoji takav provider, onda će povratna vrednost biti `defaultValue` koju ste prosledili u [`createContext`](/reference/react/createContext) za taj context. Povratna vrednost je uvek ažurna. React automatski ponovo renderuje komponente koje čitaju neki context koji se promenio. -#### Caveats {/*caveats*/} +#### Upozorenja {/*caveats*/} -* `useContext()` call in a component is not affected by providers returned from the *same* component. The corresponding `` **needs to be *above*** the component doing the `useContext()` call. -* React **automatically re-renders** all the children that use a particular context starting from the provider that receives a different `value`. The previous and the next values are compared with the [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) comparison. Skipping re-renders with [`memo`](/reference/react/memo) does not prevent the children receiving fresh context values. -* If your build system produces duplicates modules in the output (which can happen with symlinks), this can break context. Passing something via context only works if `SomeContext` that you use to provide context and `SomeContext` that you use to read it are ***exactly* the same object**, as determined by a `===` comparison. +* Na `useContext()` poziv u komponenti ne utiču provider-i vraćeni iz *iste* komponente. Odgovarajući `` **mora biti *iznad*** komponente koja poziva `useContext()`. +* React **automatski ponovo renderuje** svu decu koja koriste specifičan context počevši od provider-a koji prima drugačiji `value`. Prethodna i naredna vrednost se porede pomoću [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) poređenja. Preskakanje ponovnih rendera sa [`memo`](/reference/react/memo) ne sprečava da deca prime nove vrednosti context-a. +* Ako vaš sistem izgradnje pravi duplirane module na izlazu (što se može desiti sa simboličkim linkovima (eng. symlink)), možete slomiti context. Prosleđivanje nečega kroz context radi samo ako su `SomeContext` koji koristite za pružanje context-a i `SomeContext` koji koristite za njegovo čitanje ***potpuno* isti objekat**, utvrđeno `===` poređenjem. --- -## Usage {/*usage*/} +## Upotreba {/*usage*/} -### Passing data deeply into the tree {/*passing-data-deeply-into-the-tree*/} +### Prosleđivanje podataka duboko u stablo {/*passing-data-deeply-into-the-tree*/} -Call `useContext` at the top level of your component to read and subscribe to [context.](/learn/passing-data-deeply-with-context) +Pozovite `useContext` na vrhu vaše komponente da pročitate i da se pretplatite na [context](/learn/passing-data-deeply-with-context). ```js [[2, 4, "theme"], [1, 4, "ThemeContext"]] import { useContext } from 'react'; @@ -63,9 +63,9 @@ function Button() { // ... ``` -`useContext` returns the context value for the context you passed. To determine the context value, React searches the component tree and finds **the closest context provider above** for that particular context. +`useContext` vraća vrednost context-a za context koji ste prosledili. Da bi odredio vrednost context-a, React pretražuje stablo komponente i pronalazi **najbližeg context provider-a iznad** za taj specifični context. -To pass context to a `Button`, wrap it or one of its parent components into the corresponding context provider: +Da biste prosledili context u `Button`, obmotajte jednu od njegovih roditeljskih komponenata sa odgovarajućim context provider-om: ```js [[1, 3, "ThemeContext"], [2, 3, "\\"dark\\""], [1, 5, "ThemeContext"]] function MyPage() { @@ -77,15 +77,15 @@ function MyPage() { } function Form() { - // ... renders buttons inside ... + // ... renderuje dugmiće unutra ... } ``` -It doesn't matter how many layers of components there are between the provider and the `Button`. When a `Button` *anywhere* inside of `Form` calls `useContext(ThemeContext)`, it will receive `"dark"` as the value. +Nije bitno koliko slojeva komponenata postoji između provider-a i `Button`-a. Kada `Button` *negde* unutar `Form`-a pozove `useContext(ThemeContext)`, primiće `"dark"` kao vrednost. -`useContext()` always looks for the closest provider *above* the component that calls it. It searches upwards and **does not** consider providers in the component from which you're calling `useContext()`. +`useContext()` uvek traži najbližeg provider-a *iznad* komponente koja ga poziva. Traži nagore i **ne** razmatra provider-e u komponenti u kojoj je pozvan `useContext()`. @@ -106,9 +106,9 @@ export default function MyApp() { function Form() { return ( - - - + + + ); } @@ -175,9 +175,9 @@ function Button({ children }) { --- -### Updating data passed via context {/*updating-data-passed-via-context*/} +### Ažuriranje podataka prosleđenih kroz context {/*updating-data-passed-via-context*/} -Often, you'll want the context to change over time. To update context, combine it with [state.](/reference/react/useState) Declare a state variable in the parent component, and pass the current state down as the context value to the provider. +Često ćete želeti da se context vremenom menja. Da biste ažurirali context, kombinujte ga sa [state-om](/reference/react/useState). Deklarišite state promenljivu u roditeljskoj komponenti i prosledite trenutni state provider-u kao vrednost context-a. ```js {2} [[1, 4, "ThemeContext"], [2, 4, "theme"], [1, 11, "ThemeContext"]] function MyPage() { @@ -188,20 +188,20 @@ function MyPage() { ); } ``` -Now any `Button` inside of the provider will receive the current `theme` value. If you call `setTheme` to update the `theme` value that you pass to the provider, all `Button` components will re-render with the new `'light'` value. +Sada će svaki `Button` unutar provider-a primiti trenutnu `theme` vrednost. Ako pozovete `setTheme` da ažurirate `theme` vrednost koju prosleđujete provider-u, sve `Button` komponente će se ponovo renderovati sa novom `'light'` vrednošću. - + -#### Updating a value via context {/*updating-a-value-via-context*/} +#### Ažuriranje vrednosti kroz context {/*updating-a-value-via-context*/} -In this example, the `MyApp` component holds a state variable which is then passed to the `ThemeContext` provider. Checking the "Dark mode" checkbox updates the state. Changing the provided value re-renders all the components using that context. +U ovom primeru, `MyApp` komponenta čuva state promenljivu koja se prosleđuje u `ThemeContext` provider. Štikliranjem "Tamni režim" checkbox-a, ažurira se state. Promena pružene vrednosti ponovo renderuje sve komponente koje koriste taj context. @@ -223,7 +223,7 @@ export default function MyApp() { setTheme(e.target.checked ? 'dark' : 'light') }} /> - Use dark mode + Koristi tamni režim ) @@ -231,9 +231,9 @@ export default function MyApp() { function Form({ children }) { return ( - - - + + + ); } @@ -299,13 +299,13 @@ function Button({ children }) { -Note that `value="dark"` passes the `"dark"` string, but `value={theme}` passes the value of the JavaScript `theme` variable with [JSX curly braces.](/learn/javascript-in-jsx-with-curly-braces) Curly braces also let you pass context values that aren't strings. +Primetite da `value="dark"` prosleđuje `"dark"` string, a `value={theme}` prosleđuje vrednost JavaScript-ove `theme` promenljive sa [JSX vitičastim zagradama](/learn/javascript-in-jsx-with-curly-braces). Vitičaste zagrade vam takođe omogućavaju da context-u prosledite vrednosti koje nisu stringovi. -#### Updating an object via context {/*updating-an-object-via-context*/} +#### Ažuriranje objekta kroz context {/*updating-an-object-via-context*/} -In this example, there is a `currentUser` state variable which holds an object. You combine `{ currentUser, setCurrentUser }` into a single object and pass it down through the context inside the `value={}`. This lets any component below, such as `LoginButton`, read both `currentUser` and `setCurrentUser`, and then call `setCurrentUser` when needed. +U ovom primeru postoji `currentUser` state promenljiva koja čuva objekat. Kombinujete `{ currentUser, setCurrentUser }` u jedan objekat i prosleđujete ga kroz context unutar `value={}`. Ovo omogućava komponentama ispod, poput `LoginButton`-a, da čitaju i `currentUser` i `setCurrentUser` i da pozivaju `setCurrentUser` kada im je potrebno. @@ -330,7 +330,7 @@ export default function MyApp() { function Form({ children }) { return ( - + ); @@ -343,13 +343,13 @@ function LoginButton() { } = useContext(CurrentUserContext); if (currentUser !== null) { - return

You logged in as {currentUser.name}.

; + return

Ulogovan si kao {currentUser.name}.

; } return ( + }}>Uloguj se kao Advika ); } @@ -395,9 +395,9 @@ label { -#### Multiple contexts {/*multiple-contexts*/} +#### Više context-a {/*multiple-contexts*/} -In this example, there are two independent contexts. `ThemeContext` provides the current theme, which is a string, while `CurrentUserContext` holds the object representing the current user. +U ovom primeru postoje dva nezavisna context-a. `ThemeContext` pruža trenutnu temu, koja je string, dok `CurrentUserContext` čuva objekat koji predstavlja trenutnog korisnika. @@ -427,7 +427,7 @@ export default function MyApp() { setTheme(e.target.checked ? 'dark' : 'light') }} /> - Use dark mode + Koristi tamni režim @@ -437,7 +437,7 @@ export default function MyApp() { function WelcomePanel({ children }) { const {currentUser} = useContext(CurrentUserContext); return ( - + {currentUser !== null ? : @@ -449,7 +449,7 @@ function WelcomePanel({ children }) { function Greeting() { const {currentUser} = useContext(CurrentUserContext); return ( -

You logged in as {currentUser.name}.

+

Ulogovan si kao {currentUser.name}.

) } @@ -461,7 +461,7 @@ function LoginForm() { return ( <> ); @@ -612,7 +612,7 @@ function MyProviders({ children, theme, setTheme }) { function WelcomePanel({ children }) { const {currentUser} = useContext(CurrentUserContext); return ( - + {currentUser !== null ? : @@ -624,7 +624,7 @@ function WelcomePanel({ children }) { function Greeting() { const {currentUser} = useContext(CurrentUserContext); return ( -

You logged in as {currentUser.name}.

+

Ulogovan si kao {currentUser.name}.

) } @@ -636,7 +636,7 @@ function LoginForm() { return ( <> ); @@ -947,25 +947,25 @@ ul, li { margin: 0; padding: 0; } --- -### Specifying a fallback default value {/*specifying-a-fallback-default-value*/} +### Specificiranje default vrednosti {/*specifying-a-fallback-default-value*/} -If React can't find any providers of that particular context in the parent tree, the context value returned by `useContext()` will be equal to the default value that you specified when you [created that context](/reference/react/createContext): +Ako React ne može da pronađe nijednog provider-a za taj specifični context u stablu roditelja, vrednost context-a koju vraća `useContext()` će biti jednaka default vrednosti koju ste specificirali prilikom [kreiranja tog context-a](/reference/react/createContext): ```js [[1, 1, "ThemeContext"], [3, 1, "null"]] const ThemeContext = createContext(null); ``` -The default value **never changes**. If you want to update context, use it with state as [described above.](#updating-data-passed-via-context) +Default vrednost se **nikad ne menja**. Ako želite ažurirati context, koristite ga uz state kao što je [gore opisano](#updating-data-passed-via-context). -Often, instead of `null`, there is some more meaningful value you can use as a default, for example: +Često, umesto `null`, postoji značajnija vrednost koju možete koristiti kao default, na primer: ```js [[1, 1, "ThemeContext"], [3, 1, "light"]] const ThemeContext = createContext('light'); ``` -This way, if you accidentally render some component without a corresponding provider, it won't break. This also helps your components work well in a test environment without setting up a lot of providers in the tests. +Na ovaj način, ako slučajno renderujete neku komponentu bez odgovarajućeg provider-a, sve će raditi. Ovo takođe pomaže vašim komponentama da rade dobro u testnom okruženju bez podešavanja mnogo provider-a u testovima. -In the example below, the "Toggle theme" button is always light because it's **outside any theme context provider** and the default context theme value is `'light'`. Try editing the default theme to be `'dark'`. +U primeru ispod, "Promeni temu" dugme je uvek svetlo jer je **izvan svih provider-a theme context-a**, a default vrednost za context je `'light'`. Probajte da promenite default temu da bude `'dark'`. @@ -984,7 +984,7 @@ export default function MyApp() { ) @@ -992,9 +992,9 @@ export default function MyApp() { function Form({ children }) { return ( - - - + + + ); } @@ -1062,9 +1062,9 @@ function Button({ children, onClick }) { --- -### Overriding context for a part of the tree {/*overriding-context-for-a-part-of-the-tree*/} +### Override-ovanje context-a za deo stabla {/*overriding-context-for-a-part-of-the-tree*/} -You can override the context for a part of the tree by wrapping that part in a provider with a different value. +Možete override-ovati context za deo stabla obmotavanjem tog dela sa provider-om koji ima drugačiju vrednost. ```js {3,5} @@ -1076,13 +1076,13 @@ You can override the context for a part of the tree by wrapping that part in a p ``` -You can nest and override providers as many times as you need. +Možete ugnježdavati i override-ovati provider-e koliko god puta želite. - + -#### Overriding a theme {/*overriding-a-theme*/} +#### Override-ovanje teme {/*overriding-a-theme*/} -Here, the button *inside* the `Footer` receives a different context value (`"light"`) than the buttons outside (`"dark"`). +Ovde, dugme *unutar* `Footer`-a prima drugačiju vrednost context-a (`"light"`) od dugmića izvan (`"dark"`). @@ -1101,9 +1101,9 @@ export default function MyApp() { function Form() { return ( - - - + + +