Skip to content

Commit f01feda

Browse files
authored
Merge pull request #85 from reactjs/features/importing-and-exporting-components
2 parents 6f3f6f8 + 53c6b8f commit f01feda

File tree

1 file changed

+70
-72
lines changed

1 file changed

+70
-72
lines changed

src/content/learn/importing-and-exporting-components.md

+70-72
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
---
2-
title: Importing and Exporting Components
2+
title: Import-ovanje i export-ovanje komponenata
33
---
44

55
<Intro>
66

7-
The magic of components lies in their reusability: you can create components that are composed of other components. But as you nest more and more components, it often makes sense to start splitting them into different files. This lets you keep your files easy to scan and reuse components in more places.
7+
Magija komponenata leži u njihovoj upotrebljivosti: možete kreirati komponente koje se sastoje iz drugih komponenata. Međutim, ugnježdavanjem novih i novih komponenata, često ima smisla započeti njihovu podelu u različite fajlove. Ovo vam omogućava da fajlove lakše skenirate i koristite komponente na više mesta.
88

99
</Intro>
1010

1111
<YouWillLearn>
1212

13-
* What a root component file is
14-
* How to import and export a component
15-
* When to use default and named imports and exports
16-
* How to import and export multiple components from one file
17-
* How to split components into multiple files
13+
* Šta je to fajl root komponente
14+
* Kako da import-ujete i export-ujete komponentu
15+
* Kada da koristite default i imenovane import-e i export-e
16+
* Kako da import-ujete i export-ujete više komponenata iz jednog fajla
17+
* Kako da podelite komponente u više fajlova
1818

1919
</YouWillLearn>
2020

21-
## The root component file {/*the-root-component-file*/}
21+
## Fajl root komponente {/*the-root-component-file*/}
2222

23-
In [Your First Component](/learn/your-first-component), you made a `Profile` component and a `Gallery` component that renders it:
23+
U [Vaša prva komponenta](/learn/your-first-component), napravili ste `Profile` komponentu i `Gallery` komponentu koja je renderuje:
2424

2525
<Sandpack>
2626

@@ -37,7 +37,7 @@ function Profile() {
3737
export default function Gallery() {
3838
return (
3939
<section>
40-
<h1>Amazing scientists</h1>
40+
<h1>Zadivljujući naučnici</h1>
4141
<Profile />
4242
<Profile />
4343
<Profile />
@@ -52,17 +52,17 @@ img { margin: 0 10px 10px 0; height: 90px; }
5252

5353
</Sandpack>
5454

55-
These currently live in a **root component file,** named `App.js` in this example. Depending on your setup, your root component could be in another file, though. If you use a framework with file-based routing, such as Next.js, your root component will be different for every page.
55+
One trenutno žive u **fajlu root komponente**, koji se u ovom primeru zove `App.js`. U zavisnosti od vaših podešavanja, root komponenta može biti i u drugom fajlu. Ako koristite neki framework sa fajl rutiranjem, kao što je Next.js, vaša root komponenta će biti drugačija na svakoj stranici.
5656

57-
## Exporting and importing a component {/*exporting-and-importing-a-component*/}
57+
## Export-ovanje i import-ovanje komponente {/*exporting-and-importing-a-component*/}
5858

59-
What if you want to change the landing screen in the future and put a list of science books there? Or place all the profiles somewhere else? It makes sense to move `Gallery` and `Profile` out of the root component file. This will make them more modular and reusable in other files. You can move a component in three steps:
59+
Šta ako želite da promenite landing stranicu i prikažete listu naučnih knjiga na njoj? Ili da postavite sve profile na neko drugo mesto? Ima smisla pomeriti `Gallery` i `Profile` izvan fajla root komponente. To će im omogućiti da budu modularnije i reusable. Komponentu možete pomeriti u tri koraka:
6060

61-
1. **Make** a new JS file to put the components in.
62-
2. **Export** your function component from that file (using either [default](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_the_default_export) or [named](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_named_exports) exports).
63-
3. **Import** it in the file where you’ll use the component (using the corresponding technique for importing [default](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#importing_defaults) or [named](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#import_a_single_export_from_a_module) exports).
61+
1. **Napravite** novi JS fajl gde ćete smestiti komponente.
62+
2. **Export-ujte** vašu funkciju komponente iz tog fajla (koristeći ili [default](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_the_default_export) ili [imenovane](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_named_exports) export-e).
63+
3. **Import-ujte** ih u fajl gde ćete ih koristiti (pomoću odgovarajuće tehnike import-ovanja [default](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#importing_defaults) ili [imenovanih](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#import_a_single_export_from_a_module) export-a).
6464

65-
Here both `Profile` and `Gallery` have been moved out of `App.js` into a new file called `Gallery.js`. Now you can change `App.js` to import `Gallery` from `Gallery.js`:
65+
Sada su i `Profile` i `Gallery` pomereni iz `App.js` u fajl pod imenom `Gallery.js`. Sada možete promeniti `App.js` da import-uje `Gallery` iz `Gallery.js`:
6666

6767
<Sandpack>
6868

@@ -89,7 +89,7 @@ function Profile() {
8989
export default function Gallery() {
9090
return (
9191
<section>
92-
<h1>Amazing scientists</h1>
92+
<h1>Zadivljujući naučnici</h1>
9393
<Profile />
9494
<Profile />
9595
<Profile />
@@ -104,82 +104,82 @@ img { margin: 0 10px 10px 0; height: 90px; }
104104

105105
</Sandpack>
106106

107-
Notice how this example is broken down into two component files now:
107+
Primetite da je primer sada razdvojen na dva fajla:
108108

109109
1. `Gallery.js`:
110-
- Defines the `Profile` component which is only used within the same file and is not exported.
111-
- Exports the `Gallery` component as a **default export.**
110+
- Definiše `Profile` komponentu koja se koristi samo u okviru istog fajla i nije export-ovana.
111+
- Export-uje `Gallery` komponentu kao **default export**.
112112
2. `App.js`:
113-
- Imports `Gallery` as a **default import** from `Gallery.js`.
114-
- Exports the root `App` component as a **default export.**
113+
- Import-uje `Gallery` kao **default import** iz `Gallery.js`.
114+
- Export-uje root `App` komponentu kao **default export**.
115115

116116

117117
<Note>
118118

119-
You may encounter files that leave off the `.js` file extension like so:
119+
Možete se susresti sa fajlovima koji ne koriste `.js` ekstenziju:
120120

121121
```js
122122
import Gallery from './Gallery';
123123
```
124124

125-
Either `'./Gallery.js'` or `'./Gallery'` will work with React, though the former is closer to how [native ES Modules](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Modules) work.
125+
I `'./Gallery.js'` i `'./Gallery'` će raditi u React-u, ali je prvi način bliži tome kako [native ES moduli](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Modules) rade.
126126

127127
</Note>
128128

129129
<DeepDive>
130130

131-
#### Default vs named exports {/*default-vs-named-exports*/}
131+
#### Default vs imenovani export-i {/*default-vs-named-exports*/}
132132

133-
There are two primary ways to export values with JavaScript: default exports and named exports. So far, our examples have only used default exports. But you can use one or both of them in the same file. **A file can have no more than one _default_ export, but it can have as many _named_ exports as you like.**
133+
Postoje dva primarna načina za export-ovanje vrednosti u JavaScript-u: default export-i i imenovani export-i. Naši primeri su do sad koristili samo default export-e. Možete koristiti jedan ili oba načina u istom fajlu. **Fajl ne sme imati više od jednog _default_ export-a, ali može imati koliko god želite _imenovanih_ export-a.**
134134

135-
![Default and named exports](/images/docs/illustrations/i_import-export.svg)
135+
![Default i imenovani export-i](/images/docs/illustrations/i_import-export.svg)
136136

137-
How you export your component dictates how you must import it. You will get an error if you try to import a default export the same way you would a named export! This chart can help you keep track:
137+
Način na koji export-ujete komponentu diktira način za njeno import-ovanje. Dobićete grešku ako pokušate da import-ujete default export na isti način kao i imenovani export! Ova tabela vam može pomoći da bolje razumete:
138138

139-
| Syntax | Export statement | Import statement |
140-
| ----------- | ----------- | ----------- |
141-
| Default | `export default function Button() {}` | `import Button from './Button.js';` |
142-
| Named | `export function Button() {}` | `import { Button } from './Button.js';` |
139+
| Sintaksa | Export iskaz | Import iskaz |
140+
| ----------- | ----------- | ----------- |
141+
| Default | `export default function Button() {}` | `import Button from './Button.js';` |
142+
| Imenovano | `export function Button() {}` | `import { Button } from './Button.js';` |
143143

144-
When you write a _default_ import, you can put any name you want after `import`. For example, you could write `import Banana from './Button.js'` instead and it would still provide you with the same default export. In contrast, with named imports, the name has to match on both sides. That's why they are called _named_ imports!
144+
Kada koristite _default_ import, možete staviti bilo koje ime nakon `import`-a. Na primer, možete napisati `import Banana from './Button.js'`, ali ćete i dalje dobiti isti default export. Nasuprot tome, sa imenovanim import-ima, ime mora da se poklapa na obe strane. Zato se i nazivaju _imenovani_ import-i!
145145

146-
**People often use default exports if the file exports only one component, and use named exports if it exports multiple components and values.** Regardless of which coding style you prefer, always give meaningful names to your component functions and the files that contain them. Components without names, like `export default () => {}`, are discouraged because they make debugging harder.
146+
**Ljudi često koriste default export-e ako fajl export-uje samo jednu komponentu, a imenovane export-e ako fajl export-uje više komponenata i vrednosti.** Koji god stil kodiranja da preferirate, uvek dajte smislena imena funkcijama komponenata i fajlovima u kojima se nalaze. Komponente bez imena poput `export default () => {}` ne bi trebale da se koriste jer otežavaju debug-ovanje.
147147

148148
</DeepDive>
149149

150-
## Exporting and importing multiple components from the same file {/*exporting-and-importing-multiple-components-from-the-same-file*/}
150+
## Export-ovanje i import-ovanje više komponenata iz istog fajla {/*exporting-and-importing-multiple-components-from-the-same-file*/}
151151

152-
What if you want to show just one `Profile` instead of a gallery? You can export the `Profile` component, too. But `Gallery.js` already has a *default* export, and you can't have _two_ default exports. You could create a new file with a default export, or you could add a *named* export for `Profile`. **A file can only have one default export, but it can have numerous named exports!**
152+
Šta ako želite prikazati samo jedan `Profile` umesto galerije? Možete export-ovati `Profile` komponentu takođe. Ali `Gallery.js` već ima *default* export, a ne možete imati _dva_ default export-a. Možete kreirati novi fajl sa default export-om, ili možete dodati *imenovani* export za `Profile`. **Fajl može imati samo jedan default export, ali može imati bezbroj imenovanih export-a!**
153153

154154
<Note>
155155

156-
To reduce the potential confusion between default and named exports, some teams choose to only stick to one style (default or named), or avoid mixing them in a single file. Do what works best for you!
156+
Da bi izbegli zabunu između default i imenovanih export-a, neki timovi odluče da se drže jednog načina (default ili imenovani), ili izbegavaju upotrebu oba u istom fajlu. Koristite ono što vam najviše odgovara!
157157

158158
</Note>
159159

160-
First, **export** `Profile` from `Gallery.js` using a named export (no `default` keyword):
160+
Prvo, **export-ujte** `Profile` iz `Gallery.js` upotrebom imenovanog export-a (bez ključne reči `default`):
161161

162162
```js
163163
export function Profile() {
164164
// ...
165165
}
166166
```
167167

168-
Then, **import** `Profile` from `Gallery.js` to `App.js` using a named import (with the curly braces):
168+
Nakon toga, **import-ujte** `Profile` iz `Gallery.js` u `App.js` upotrebom imenovanog import-a (sa vitičastim zagradama):
169169

170170
```js
171171
import { Profile } from './Gallery.js';
172172
```
173173

174-
Finally, **render** `<Profile />` from the `App` component:
174+
Na kraju, **renderujte** `<Profile />` u `App` komponenti:
175175

176176
```js
177177
export default function App() {
178178
return <Profile />;
179179
}
180180
```
181181

182-
Now `Gallery.js` contains two exports: a default `Gallery` export, and a named `Profile` export. `App.js` imports both of them. Try editing `<Profile />` to `<Gallery />` and back in this example:
182+
Sada `Gallery.js` sadrži dva export-a: default `Gallery` export i imenovani `Profile` export. `App.js` ih oba import-uje. Probajte da menjate `<Profile />` i `<Gallery />` naizmenično:
183183

184184
<Sandpack>
185185

@@ -207,7 +207,7 @@ export function Profile() {
207207
export default function Gallery() {
208208
return (
209209
<section>
210-
<h1>Amazing scientists</h1>
210+
<h1>Zadivljujući naučnici</h1>
211211
<Profile />
212212
<Profile />
213213
<Profile />
@@ -222,47 +222,45 @@ img { margin: 0 10px 10px 0; height: 90px; }
222222

223223
</Sandpack>
224224

225-
Now you're using a mix of default and named exports:
225+
Sada koristite kombinaciju default i imenovanih export-a:
226226

227227
* `Gallery.js`:
228-
- Exports the `Profile` component as a **named export called `Profile`.**
229-
- Exports the `Gallery` component as a **default export.**
228+
- Export-uje `Profile` komponentu kao **imenovani export pod imenom `Profile`**.
229+
- Export-uje `Gallery` komponentu kao **default export**.
230230
* `App.js`:
231-
- Imports `Profile` as a **named import called `Profile`** from `Gallery.js`.
232-
- Imports `Gallery` as a **default import** from `Gallery.js`.
233-
- Exports the root `App` component as a **default export.**
231+
- Import-uje `Profile` kao **imenovani import pod imenom `Profile`** iz `Gallery.js`.
232+
- Import-uje `Gallery` kao **default import** iz `Gallery.js`.
233+
- Export-uje root `App` komponentu kao **default export**.
234234

235235
<Recap>
236236

237-
On this page you learned:
237+
Na ovoj stranici ste naučili:
238238

239-
* What a root component file is
240-
* How to import and export a component
241-
* When and how to use default and named imports and exports
242-
* How to export multiple components from the same file
239+
* Šta je to fajl root komponente
240+
* Kako da import-ujete i export-ujete komponentu
241+
* Kada i kako da koristite default i imenovane import-e i export-e
242+
* Kako da export-ujete više komponenata iz istog fajla
243243

244244
</Recap>
245245

246-
247-
248246
<Challenges>
249247

250-
#### Split the components further {/*split-the-components-further*/}
248+
#### Delite komponente dalje {/*split-the-components-further*/}
251249

252-
Currently, `Gallery.js` exports both `Profile` and `Gallery`, which is a bit confusing.
250+
Trenutno, `Gallery.js` export-uje i `Profile` i `Gallery`, što je malo zbunjujuće.
253251

254-
Move the `Profile` component to its own `Profile.js`, and then change the `App` component to render both `<Profile />` and `<Gallery />` one after another.
252+
Pomerite `Profile` komponentu u zaseban fajl `Profile.js`, a nakon toga izmenite `App` komponentu da renderuje i `<Profile />` i `<Gallery />` komponente jednu za drugom.
255253

256-
You may use either a default or a named export for `Profile`, but make sure that you use the corresponding import syntax in both `App.js` and `Gallery.js`! You can refer to the table from the deep dive above:
254+
Možete koristiti i default i imenovani export za `Profile`, ali se potrudite da iskoristite odgovarajuću import sintaksu i u `App.js` i u `Gallery.js`! Možete se osloniti na već spomenutu tabelu:
257255

258-
| Syntax | Export statement | Import statement |
259-
| ----------- | ----------- | ----------- |
260-
| Default | `export default function Button() {}` | `import Button from './Button.js';` |
261-
| Named | `export function Button() {}` | `import { Button } from './Button.js';` |
256+
| Sintaksa | Export iskaz | Import iskaz |
257+
| ----------- | ----------- | ----------- |
258+
| Default | `export default function Button() {}` | `import Button from './Button.js';` |
259+
| Imenovano | `export function Button() {}` | `import { Button } from './Button.js';` |
262260

263261
<Hint>
264262

265-
Don't forget to import your components where they are called. Doesn't `Gallery` use `Profile`, too?
263+
Ne zaboravite da import-ujete komponente na mestu gde ih koristite. I `Gallery` koristi `Profile`, zar ne?
266264

267265
</Hint>
268266

@@ -282,7 +280,7 @@ export default function App() {
282280
```
283281

284282
```js src/Gallery.js active
285-
// Move me to Profile.js!
283+
// Pomerite me u Profile.js!
286284
export function Profile() {
287285
return (
288286
<img
@@ -295,7 +293,7 @@ export function Profile() {
295293
export default function Gallery() {
296294
return (
297295
<section>
298-
<h1>Amazing scientists</h1>
296+
<h1>Zadivljujući naučnici</h1>
299297
<Profile />
300298
<Profile />
301299
<Profile />
@@ -313,11 +311,11 @@ img { margin: 0 10px 10px 0; height: 90px; }
313311

314312
</Sandpack>
315313

316-
After you get it working with one kind of exports, make it work with the other kind.
314+
Nakon što uspete sa jednim tipom export-a, napravite da radi i sa drugim.
317315

318316
<Solution>
319317

320-
This is the solution with named exports:
318+
Ovo je rešenje sa imenovanim export-ima:
321319

322320
<Sandpack>
323321

@@ -341,7 +339,7 @@ import { Profile } from './Profile.js';
341339
export default function Gallery() {
342340
return (
343341
<section>
344-
<h1>Amazing scientists</h1>
342+
<h1>Zadivljujući naučnici</h1>
345343
<Profile />
346344
<Profile />
347345
<Profile />
@@ -367,7 +365,7 @@ img { margin: 0 10px 10px 0; height: 90px; }
367365

368366
</Sandpack>
369367

370-
This is the solution with default exports:
368+
Ovo je rešenje sa default export-ima:
371369

372370
<Sandpack>
373371

@@ -391,7 +389,7 @@ import Profile from './Profile.js';
391389
export default function Gallery() {
392390
return (
393391
<section>
394-
<h1>Amazing scientists</h1>
392+
<h1>Zadivljujući naučnici</h1>
395393
<Profile />
396394
<Profile />
397395
<Profile />

0 commit comments

Comments
 (0)