|
1 | 1 | ---
|
2 |
| -title: Add React to an Existing Project |
| 2 | +title: Innsetning á React í tiltæku verkefni |
3 | 3 | ---
|
4 | 4 |
|
5 | 5 | <Intro>
|
6 | 6 |
|
7 |
| -If you want to add some interactivity to your existing project, you don't have to rewrite it in React. Add React to your existing stack, and render interactive React components anywhere. |
| 7 | +Þú þarft ekki að endurskrifa tiltækt verkefni til að auka gagnvirknina. Þú getur bætt React við núverandi tæknistafla og birt React íhluti hvar sem er. |
8 | 8 |
|
9 | 9 | </Intro>
|
10 | 10 |
|
11 | 11 | <Note>
|
12 | 12 |
|
13 |
| -**You need to install [Node.js](https://nodejs.org/en/) for local development.** Although you can [try React](/learn/installation#try-react) online or with a simple HTML page, realistically most JavaScript tooling you'll want to use for development requires Node.js. |
| 13 | +**Þú verður að innsetja [Node.js](https://nodejs.org/en/) fyrir staðbundna þróun.** Þrátt fyrir að þú getir [prófað React](/learn/installation#try-react) á Netinu eða með einfaldri HTML síðu þá munu flest tólin sem þú þarft í þróunarumhverfinu þurfa á Node.js að halda. |
14 | 14 |
|
15 | 15 | </Note>
|
16 | 16 |
|
17 |
| -## Using React for an entire subroute of your existing website {/*using-react-for-an-entire-subroute-of-your-existing-website*/} |
| 17 | +## Notkun á React fyrir undirsíðu á núverandi síðu {/*using-react-for-an-entire-subroute-of-your-existing-website*/} |
18 | 18 |
|
19 |
| -Let's say you have an existing web app at `example.com` built with another server technology (like Rails), and you want to implement all routes starting with `example.com/some-app/` fully with React. |
| 19 | +Gefum okkur að þú sért með tiltæka vefþjónustu á `example.com` sem var skrifuð með bakendaþjónustu eins og Rails, og þú viljir útfæra allar beiningar sem byrja á `example.com/eitthvað-forrit` með React. |
20 | 20 |
|
21 |
| -Here's how we recommend to set it up: |
| 21 | +Þá mælum við með þessari nálgun: |
22 | 22 |
|
23 |
| -1. **Build the React part of your app** using one of the [React-based frameworks](/learn/start-a-new-react-project). |
24 |
| -2. **Specify `/some-app` as the *base path*** in your framework's configuration (here's how: [Next.js](https://nextjs.org/docs/api-reference/next.config.js/basepath), [Gatsby](https://www.gatsbyjs.com/docs/how-to/previews-deploys-hosting/path-prefix/)). |
25 |
| -3. **Configure your server or a proxy** so that all requests under `/some-app/` are handled by your React app. |
| 23 | +1. **Smíðaðu React hlutann** með einhverju [React rammakerfi](/learn/start-a-new-react-project). |
| 24 | +2. **Tilgreindu `/eitthvað-forrit` sem *grunnslóð*** í stillingum rammans sem þú valdir (sjá hér: [Next.js](https://nextjs.org/docs/api-reference/next.config.js/basepath) eða [Gatsby](https://www.gatsbyjs.com/docs/how-to/previews-deploys-hosting/path-prefix/)). |
| 25 | +3. **Stilltu vefþjóninn eða vefselið** á þann veg að allar beiðnir sem hefjist á `/eitthvað-forrit/` séu birtar í React forritinu þínu. |
26 | 26 |
|
27 |
| -This ensures the React part of your app can [benefit from the best practices](/learn/start-a-new-react-project#can-i-use-react-without-a-framework) baked into those frameworks. |
| 27 | +Þetta tryggir að React hlutinn í vefsíðunni þinni [fylgi þróunarstöðlunum](/learn/start-a-new-react-project#can-i-use-react-without-a-framework) sem eru innbyggðir í þessum römmum. |
28 | 28 |
|
29 |
| -Many React-based frameworks are full-stack and let your React app take advantage of the server. However, you can use the same approach even if you can't or don't want to run JavaScript on the server. In that case, serve the HTML/CSS/JS export ([`next export` output](https://nextjs.org/docs/advanced-features/static-html-export) for Next.js, default for Gatsby) at `/some-app/` instead. |
| 29 | +Sum React rammakerfi eru heilstafla (e. full-stack) og gera þér kleift að skrifa bakendakóða líka. Hvort sem það er eitthvað sem þú vilt nýta þer eða ekki þá geturðu fylgt sömu nálgunum. Til dæmis er hægt að birta kyrrlegt HTML, CSS, og JS ([`next export` úttak](https://nextjs.org/docs/advanced-features/static-html-export) í Next.js, sjálfgefið í Gatsby) á síðunni `/eitthvað-forrit`. |
30 | 30 |
|
31 |
| -## Using React for a part of your existing page {/*using-react-for-a-part-of-your-existing-page*/} |
| 31 | +## Notkun React á hluta vefsíðu {/*using-react-for-a-part-of-your-existing-page*/} |
32 | 32 |
|
33 |
| -Let's say you have an existing page built with another technology (either a server one like Rails, or a client one like Backbone), and you want to render interactive React components somewhere on that page. That's a common way to integrate React--in fact, it's how most React usage looked at Meta for many years! |
| 33 | +Gefum okkur að þú sért með vefsíðu sem er skrifuð í annarri tækni (annaðvort bakendatækni eins og Rails eða framendatækni eins og Backbone), og að þú viljir birta gagnvirka React íhluti einhvers staðar á síðunni. Þetta er tiltölulega algeng nálgun til að nota React--í raun var það á tímabili nákvæmlega þannig sem Meta notaði React! |
34 | 34 |
|
35 |
| -You can do this in two steps: |
| 35 | +Þú getur gert þetta í tveimur skrefum: |
36 | 36 |
|
37 |
| -1. **Set up a JavaScript environment** that lets you use the [JSX syntax](/learn/writing-markup-with-jsx), split your code into modules with the [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) / [`export`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export) syntax, and use packages (for example, React) from the [npm](https://www.npmjs.com/) package registry. |
38 |
| -2. **Render your React components** where you want to see them on the page. |
| 37 | +1. **Útbúðu JavaScript umhverfi** sem gerir þér kleift að nota [JSX málskipan](/learn/writing-markup-with-jsx), deila kóðanum þínum í einingar með [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) og [`export`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export) málskipan, og notaðu forritasöfn (til dæmis, React) frá [npm](https://www.npmjs.com/) pakkaskránni. |
| 38 | +2. **Birtu React íhlutina þína** þar sem þú vilt hafa þá á síðunni. |
39 | 39 |
|
40 |
| -The exact approach depends on your existing page setup, so let's walk through some details. |
| 40 | +Nákvæm útfærsla á þessu veltur á núverandi uppsetningu hjá þér, svo við skulum fara aðeins nánar út í smáatriðin. |
41 | 41 |
|
42 |
| -### Step 1: Set up a modular JavaScript environment {/*step-1-set-up-a-modular-javascript-environment*/} |
| 42 | +### Fyrsta skref: Skiptu JavaScript umhverfinu þínu í einingar {/*step-1-set-up-a-modular-javascript-environment*/} |
43 | 43 |
|
44 |
| -A modular JavaScript environment lets you write your React components in individual files, as opposed to writing all of your code in a single file. It also lets you use all the wonderful packages published by other developers on the [npm](https://www.npmjs.com/) registry--including React itself! How you do this depends on your existing setup: |
| 44 | +Með því að skipta JavaScript kóða í einingar er hægt að nota margar skrár í stað þess að skrifa allan kóðann í eina skrá. Það gerir þér líka kleift að nota öll forritasöfnin sem eru aðgengileg á [npm](https://www.npmjs.com/) pakkaskránni--þar á meðal React sjálft! Nálgunin veltur á núverandi uppsetningu: |
45 | 45 |
|
46 |
| -* **If your app is already split into files that use `import` statements,** try to use the setup you already have. Check whether writing `<div />` in your JS code causes a syntax error. If it causes a syntax error, you might need to [transform your JavaScript code with Babel](https://babeljs.io/setup), and enable the [Babel React preset](https://babeljs.io/docs/babel-preset-react) to use JSX. |
| 46 | +* **Ef vefurinn þinn er nú þegar skiptur í mismunandi skrár sem nota `import` setningar** reyndu þá að halda áfram að nota sama kerfi. Athugaðu hvort það virki að skrifa `<div />` án þess að það komi málskipanvilla. Ef þetta veldur villu þá kann að vera að þú þurfir að [umbreyta JavaScript kóðanum með Babel](https://babeljs.io/setup), og virkja [Babel React forstillinguna](https://babeljs.io/docs/babel-preset-react) til að nota JSX. |
47 | 47 |
|
48 |
| -* **If your app doesn't have an existing setup for compiling JavaScript modules,** set it up with [Vite](https://vitejs.dev/). The Vite community maintains [many integrations with backend frameworks](https://github.com/vitejs/awesome-vite#integrations-with-backends), including Rails, Django, and Laravel. If your backend framework is not listed, [follow this guide](https://vitejs.dev/guide/backend-integration.html) to manually integrate Vite builds with your backend. |
| 48 | +* **Ef vefurinn þinn er ekki með neitt kerfi til að þýða JavaScript einingar** þá skaltu prófa að nota [Vite](https://vitejs.dev/) til þess. Vite samfélagið viðheldur [margs konar samtengingum við bakendaramma](https://github.com/vitejs/awesome-vite#integrations-with-backends), m.a. Rails, Django, og Laravel. Ef bakendaramminn þinn er ekki útlistaður þá skaltu [fylgja þessum leiðbeiningum](https://vitejs.dev/guide/backend-integration.html) til að tengja Vite við bakendann þinn handvirkt. |
49 | 49 |
|
50 |
| -To check whether your setup works, run this command in your project folder: |
| 50 | +Til að kanna hvort kerfið þitt virki geturðu keyrt eftirfarandi skipun í aðalmöppu verkefnisins: |
51 | 51 |
|
52 | 52 | <TerminalBlock>
|
53 | 53 | npm install react react-dom
|
54 | 54 | </TerminalBlock>
|
55 | 55 |
|
56 |
| -Then add these lines of code at the top of your main JavaScript file (it might be called `index.js` or `main.js`): |
| 56 | +Bættu svo þessum kóða efst í aðal JavaScript skránni (hún ætti að heita `index.js` eða kannski `main.js`): |
57 | 57 |
|
58 | 58 | <Sandpack>
|
59 | 59 |
|
60 | 60 | ```html index.html hidden
|
61 | 61 | <!DOCTYPE html>
|
62 | 62 | <html>
|
63 |
| - <head><title>My app</title></head> |
| 63 | + <head><title>Vefurinn minn</title></head> |
64 | 64 | <body>
|
65 |
| - <!-- Your existing page content (in this example, it gets replaced) --> |
| 65 | + <!-- Núverandi efni (í þessu tilfelli skiptum við því algjörlega út) --> |
66 | 66 | </body>
|
67 | 67 | </html>
|
68 | 68 | ```
|
69 | 69 |
|
70 | 70 | ```js index.js active
|
71 | 71 | import { createRoot } from 'react-dom/client';
|
72 | 72 |
|
73 |
| -// Clear the existing HTML content |
| 73 | +// Fjarlægjum núverandi efni. |
74 | 74 | document.body.innerHTML = '<div id="app"></div>';
|
75 | 75 |
|
76 |
| -// Render your React component instead |
| 76 | +// Birtum React íhlut þess í stað. |
77 | 77 | const root = createRoot(document.getElementById('app'));
|
78 |
| -root.render(<h1>Hello, world</h1>); |
| 78 | +root.render(<h1>Halló, heimur!</h1>); |
79 | 79 | ```
|
80 | 80 |
|
81 | 81 | </Sandpack>
|
82 | 82 |
|
83 |
| -If the entire content of your page was replaced by a "Hello, world!", everything worked! Keep reading. |
| 83 | +Ef efnið á síðunni hvarf og „Halló, heimur!“ kom þess í stað þá virkaði breytingin og þú getur haldið áfram að lesa þessar leiðbeiningar. |
84 | 84 |
|
85 | 85 | <Note>
|
86 | 86 |
|
87 |
| -Integrating a modular JavaScript environment into an existing project for the first time can feel intimidating, but it's worth it! If you get stuck, try our [community resources](/community) or the [Vite Chat](https://chat.vitejs.dev/). |
| 87 | +Fyrst um sinn getur verið hálfógnvekjandi að reyna að útfæra einingamiðað JavaScript umhverfi, en það er þess virði! Prófaðu að skoða [efnið frá React samfélaginu](/community) eða [Vite Chat](https://chat.vitejs.dev/) ef þér finnst þú ekki ná árangri. |
88 | 88 |
|
89 | 89 | </Note>
|
90 | 90 |
|
91 |
| -### Step 2: Render React components anywhere on the page {/*step-2-render-react-components-anywhere-on-the-page*/} |
| 91 | +### Annað skref: Birtu React íhluti hvar sem er á síðunni {/*step-2-render-react-components-anywhere-on-the-page*/} |
92 | 92 |
|
93 |
| -In the previous step, you put this code at the top of your main file: |
| 93 | +Í skrefinu á undan settum við þennan kóða efst í aðalskránni: |
94 | 94 |
|
95 |
| -```js |
| 95 | +```js index.js active |
96 | 96 | import { createRoot } from 'react-dom/client';
|
97 | 97 |
|
98 |
| -// Clear the existing HTML content |
| 98 | +// Fjarlægjum núverandi efni. |
99 | 99 | document.body.innerHTML = '<div id="app"></div>';
|
100 | 100 |
|
101 |
| -// Render your React component instead |
| 101 | +// Birtum React íhlut þess í stað. |
102 | 102 | const root = createRoot(document.getElementById('app'));
|
103 |
| -root.render(<h1>Hello, world</h1>); |
| 103 | +root.render(<h1>Halló, heimur!</h1>); |
104 | 104 | ```
|
105 | 105 |
|
106 |
| -Of course, you don't actually want to clear the existing HTML content! |
| 106 | +En að sjálfsögðu viljum við ekki henda öllu efninu af síðunni! |
107 | 107 |
|
108 |
| -Delete this code. |
| 108 | +Eyddu þessum kóða. |
109 | 109 |
|
110 | 110 | Instead, you probably want to render your React components in specific places in your HTML. Open your HTML page (or the server templates that generate it) and add a unique [`id`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id) attribute to any tag, for example:
|
| 111 | +Þess í stað viltu sennilega birta React íhluti á ákveðnum stöðum. Opnaðu HTML síðuna (eða sniðmátið sem bakendinn útbýr) og bættu við einstöku [`id`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id) eigindi við hvaða merki sem er á síðunni, til dæmis: |
111 | 112 |
|
112 | 113 | ```html
|
113 |
| -<!-- ... somewhere in your html ... --> |
114 |
| -<nav id="navigation"></nav> |
115 |
| -<!-- ... more html ... --> |
| 114 | +<!-- ... einhvers staðar í skránni ... --> |
| 115 | +<nav id="valmynd"></nav> |
| 116 | +<!-- ... meira HTML ... --> |
116 | 117 | ```
|
117 | 118 |
|
118 |
| -This lets you find that HTML element with [`document.getElementById`](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById) and pass it to [`createRoot`](/reference/react-dom/client/createRoot) so that you can render your own React component inside: |
| 119 | +Með þessu móti geturðu fundið eininguna í HTML skránni með [`document.getElementById`](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById) og fært [`createRoot`](/reference/react-dom/client/createRoot) hana til að birta React íhlut inni í henni: |
119 | 120 |
|
120 | 121 | <Sandpack>
|
121 | 122 |
|
122 | 123 | ```html index.html
|
123 | 124 | <!DOCTYPE html>
|
124 | 125 | <html>
|
125 |
| - <head><title>My app</title></head> |
| 126 | + <head><title>Vefurinn minn</title></head> |
126 | 127 | <body>
|
127 |
| - <p>This paragraph is a part of HTML.</p> |
128 |
| - <nav id="navigation"></nav> |
129 |
| - <p>This paragraph is also a part of HTML.</p> |
| 128 | + <p>Þessi efnisgrein er hluti af HTML skránni.</p> |
| 129 | + <nav id="valmynd"></nav> |
| 130 | + <p>Þessi efnisgrein er líka hluti af HTML skránni.</p> |
130 | 131 | </body>
|
131 | 132 | </html>
|
132 | 133 | ```
|
133 | 134 |
|
134 | 135 | ```js index.js active
|
135 | 136 | import { createRoot } from 'react-dom/client';
|
136 | 137 |
|
137 |
| -function NavigationBar() { |
138 |
| - // TODO: Actually implement a navigation bar |
139 |
| - return <h1>Hello from React!</h1>; |
| 138 | +function Valmynd() { |
| 139 | + // Síðar: útfærum raunverulegu valmyndina! |
| 140 | + return <h1>Halló frá React!</h1>; |
140 | 141 | }
|
141 | 142 |
|
142 |
| -const domNode = document.getElementById('navigation'); |
143 |
| -const root = createRoot(domNode); |
144 |
| -root.render(<NavigationBar />); |
| 143 | +const domHnútur = document.getElementById('valmynd'); |
| 144 | +const rót = createRoot(domHnútur); |
| 145 | +rót.render(<Valmynd />); |
145 | 146 | ```
|
146 | 147 |
|
147 | 148 | </Sandpack>
|
148 | 149 |
|
149 |
| -Notice how the original HTML content from `index.html` is preserved, but your own `NavigationBar` React component now appears inside the `<nav id="navigation">` from your HTML. Read the [`createRoot` usage documentation](/reference/react-dom/client/createRoot#rendering-a-page-partially-built-with-react) to learn more about rendering React components inside an existing HTML page. |
| 150 | +Taktu eftir því að upprunalega HTML efnið í `index.html` er enn þar, en React íhluturinn `Valmynd` er nú innan í `<nav id="valmynd">` í HTML skránni. Lestu [skjalbúnaðinn fyrir `createRoot`](/reference/react-dom/client/createRoot#rendering-a-page-partially-built-with-react) til að kynna þér nánar hvernig hægt er að birta React íhluti inni í HTML síðu. |
150 | 151 |
|
151 |
| -When you adopt React in an existing project, it's common to start with small interactive components (like buttons), and then gradually keep "moving upwards" until eventually your entire page is built with React. If you ever reach that point, we recommend migrating to [a React framework](/learn/start-a-new-react-project) right after to get the most out of React. |
| 152 | +Það er algengt að byrja á að útfæra smærri gagnvirka íhluti eins og takka og hægt og rólega „færa sig ofar“ þar til öll síðan er útfærð í React. Ef þú nálgast það stig þá mælum við með því að flytja þig yfir í [React ramma](/learn/start-a-new-react-project) til að nýta React sem best. |
152 | 153 |
|
153 |
| -## Using React Native in an existing native mobile app {/*using-react-native-in-an-existing-native-mobile-app*/} |
| 154 | +## Notkun á React Native í tiltæku forriti á heimavangi {/*using-react-native-in-an-existing-native-mobile-app*/} |
154 | 155 |
|
155 |
| -[React Native](https://reactnative.dev/) can also be integrated into existing native apps incrementally. If you have an existing native app for Android (Java or Kotlin) or iOS (Objective-C or Swift), [follow this guide](https://reactnative.dev/docs/integration-with-existing-apps) to add a React Native screen to it. |
| 156 | +Það er einnig hægt að byrja að nota [React Native](https://reactnative.dev/) hægt og rólega. Ef þú ert með forrit fyrir Android (í Java eða Kotlin) eða iOS (í Objective-C eða Swift), [fylgdu þá þessum leiðbeiningum](https://reactnative.dev/docs/integration-with-existing-apps) til að bæta við virkni í React Native. |
0 commit comments