You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/learn/reusing-logic-with-custom-hooks.md
+40-40Lines changed: 40 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,12 +19,12 @@ React, `useState`, `useContext`, ve `useEffect` gibi birkaç yerleşik Hook ile
19
19
20
20
## Özel Hook'lar: Bileşenler arasında mantığı paylaşma {/*custom-hooks-sharing-logic-between-components*/}
21
21
22
-
Imagine you're developing an app that heavily relies on the network (as most apps do). You want to warn the user if their network connection has accidentally gone off while they were using your app. How would you go about it? It seems like you'll need two things in your component:
22
+
Ağ'a ağır bir şekilde bağlı olan bir uygulama geliştirdiğinizi hayal edin (çoğu uygulamanın bağlı olduğu gibi). Kullanıcıyı, uygulamanızı kullanırken ağ bağlantısının yanlışlıkla kapandığı durumlarda uyarmak istersiniz. Bunu nasıl yapardınız? Bileşeninizde iki şeye ihtiyacınız olduğu gibi görünüyor:
23
23
24
-
1.A piece of state that tracks whether the network is online.
25
-
2.An Effect that subscribes to the global [`online`](https://developer.mozilla.org/en-US/docs/Web/API/Window/online_event)and[`offline`](https://developer.mozilla.org/en-US/docs/Web/API/Window/offline_event)events, and updates that state.
24
+
1.Ağınızın çevrimiçi olup olmadığını izleyen bir state parçası.
25
+
2.Global [`online`](https://developer.mozilla.org/en-US/docs/Web/API/Window/online_event)ve[`offline`](https://developer.mozilla.org/en-US/docs/Web/API/Window/offline_event)olaylarına abone olan ve bu state'i güncelleyen bir Effect.
26
26
27
-
This will keep your component [synchronized](/learn/synchronizing-with-effects)with the network status. You might start with something like this:
27
+
Bu sizin bileşeninizi ağ durumu ile [senkronize](/learn/synchronizing-with-effects)tutacaktır. Şöyle bir şeyle başlayabilirsiniz:
28
28
29
29
<Sandpack>
30
30
@@ -48,17 +48,17 @@ export default function StatusBar() {
return<h1>{isOnline ?'✅ Çevrimiçi':'❌ Bağlantı kopmuş'}</h1>;
52
52
}
53
53
```
54
54
55
55
</Sandpack>
56
56
57
-
Try turning your network on and off, and notice how this `StatusBar` updates in response to your actions.
57
+
Ağınızı kapatıp açmayı deneyin ve bu `StatusBar`'ın tepki olarak nasıl güncellendiğini fark edin.
58
58
59
-
Now imagine you *also* want to use the same logic in a different component. You want to implement a Save button that will become disabled and show "Reconnecting..." instead of "Save" while the network is off.
59
+
Şimdi *ek olarak* aynı mantığı farklı bir bileşende kullanmak istediğinizi hayal edin. Ağ kapalıyken "Kaydet" yerine "Yeniden bağlanıyor..." yazan ve devre dışı bırakılan bir Kaydet düğmesi uygulamak istiyorsunuz.
60
60
61
-
To start, you can copy and paste the `isOnline` state and the Effect into `SaveButton`:
61
+
Başlangıç olarak, `isOnline` state'ini ve Effect'i `SaveButton`'a kopyalayabilirsiniz:
62
62
63
63
<Sandpack>
64
64
@@ -83,49 +83,49 @@ export default function SaveButton() {
These two components work fine, but the duplication in logic between them is unfortunate. It seems like even though they have different *visual appearance,* you want to reuse the logic between them.
101
+
Bu iki bileşen iyi çalışıyor, ancak aralarındaki mantık tekrarı talihsiz. Görünen o ki farklı *görsel görünüme* sahip olsalar da, aralarındaki mantığı yeniden kullanmak istiyorsunuz.
102
102
103
-
### Extracting your own custom Hook from a component {/*extracting-your-own-custom-hook-from-a-component*/}
103
+
### Kendi özel Hook'unuzu bir bileşenden çıkarma {/*extracting-your-own-custom-hook-from-a-component*/}
104
104
105
-
Imagine for a moment that, similar to [`useState`](/reference/react/useState)and[`useEffect`](/reference/react/useEffect), there was a built-in `useOnlineStatus` Hook. Then both of these components could be simplified and you could remove the duplication between them:
105
+
Bir an için hayal edin, [`useState`](/reference/react/useState)ve[`useEffect`](/reference/react/useEffect) gibi, yerleşik bir `useOnlineStatus` Hook'u olsaydı. O zaman bu iki bileşen de basitleştirilebilir ve aralarındaki tekrarı kaldırabilirsiniz:
Although there is no such built-in Hook, you can write it yourself. Declare a function called `useOnlineStatus`and move all the duplicated code into it from the components you wrote earlier:
128
+
Yerleşik bir Hook bulunmasa da, kendiniz yazabilirsiniz. `useOnlineStatus`adında bir fonksiyon oluşturun ve daha önce yazdığınız bileşenlerdeki tekrarlanan kodu içine taşıyın:
129
129
130
130
```js {2-16}
131
131
functionuseOnlineStatus() {
@@ -148,7 +148,7 @@ function useOnlineStatus() {
148
148
}
149
149
```
150
150
151
-
At the end of the function, return `isOnline`. This lets your components read that value:
151
+
Fonksiyonun sonunda `isOnline`'ı döndürün. Bu, bileşenlerinizin bu değeri okumasına olanak sağlar:
152
152
153
153
<Sandpack>
154
154
@@ -157,19 +157,19 @@ import { useOnlineStatus } from './useOnlineStatus.js';
@@ -209,62 +209,62 @@ export function useOnlineStatus() {
209
209
210
210
</Sandpack>
211
211
212
-
Verify that switching the network on and off updates both components.
212
+
Ağı kapatıp açmanın iki bileşeni de güncellediğini doğrulayın.
213
213
214
-
Now your components don't have as much repetitive logic. **More importantly, the code inside them describes *what they want to do* (use the online status!) rather than *how to do it* (by subscribing to the browser events).**
214
+
Şimdi bileşenleriniz çok tekrarlı mantığa sahip değil. **Daha da önemlisi, içlerindeki kod, *nasıl yapacakları*ndan (tarayıcı olaylarına abone olarak) ziyade *ne yapmak istedikleri*ni (çevrimiçi durumu kullanın!) açıklıyor.**
215
215
216
-
When you extract logic into custom Hooks, you can hide the gnarly details of how you deal with some external system or a browser API. The code of your components expresses your intent, not the implementation.
216
+
Mantığı özel Hook'lara çıkarttığınızda, bir harici sistem ya da tarayıcı API'si ile nasıl başa çıktığınızın zorlu ayrıntılarını gizleyebilirsiniz. Bileşenlerinizin kodu, uygulamanızın nasıl yerine getirdiğinden ziyade ne yapmak istediğinizi açıklar.
217
217
218
-
### Hook names always start with `use` {/*hook-names-always-start-with-use*/}
218
+
### Hook isimleri her zaman `use` ile başlar {/*hook-names-always-start-with-use*/}
219
219
220
-
React applications are built from components. Components are built from Hooks, whether built-in or custom. You'll likely often use custom Hooks created by others, but occasionally you might write one yourself!
220
+
React uygulamaları bileşenlerden oluşur. Bileşenler yerleşik veya özel olsun, Hook'lardan oluşur. Muhtemelen sıklıkla başkaları tarafından oluşturulan özel Hook'ları kullanacaksınız, ancak arada bir kendiniz de yazabilirsiniz!
221
221
222
-
You must follow these naming conventions:
222
+
Bu isimlendirme kurallarına uymalısınız:
223
223
224
-
1.**React component names must start with a capital letter,**like `StatusBar`and`SaveButton`. React components also need to return something that React knows how to display, like a piece of JSX.
225
-
2.**Hook names must start with `use`followed by a capital letter,**like [`useState`](/reference/react/useState) (built-in) or`useOnlineStatus` (custom, like earlier on the page). Hooks may return arbitrary values.
224
+
1.**React bileşenleri büyük harfle başlamalıdır,**`StatusBar`ve`SaveButton` gibi. React bileşenleri ayrıca, JSX gibi, React'in nasıl görüntüleyeceğini bildiği bir şey döndürmelidir.
225
+
2.**Hook isimleri `use`ile başlayıp büyük harfle devam etmelidir,**[`useState`](/reference/react/useState) (yerleşik) veya`useOnlineStatus` (özel, yukarıdaki örnekte olduğu gibi). Hook'lar keyfi değerler döndürebilir.
226
226
227
-
This convention guarantees that you can always look at a component and know where its state, Effects, and other React features might "hide". For example, if you see a `getColor()`function call inside your component, you can be sure that it can't possibly contain React state inside because its name doesn't start with `use`. However, a function call like `useOnlineStatus()`will most likely contain calls to other Hooks inside!
227
+
Bu kural, sizin bir bileşene her baktığınızda onun state, efektleri ve diğer React özelliklerinin nerede "saklanabileceğini" bilmenizi garanti eder. Örneğin, bileşeninizde `getColor()`fonksiyonu çağrısı görürseniz, adının `use` ile başlamadığı için içinde React state'i içeremeyeceğinden emin olabilirsiniz. Ancak, `useOnlineStatus()`gibi bir fonksiyon çağrısı büyük olasılıkla içinde başka Hook'lara çağrı içerecektir!
228
228
229
229
<Note>
230
230
231
-
If your linter is [configured for React,](/learn/editor-setup#linting)it will enforce this naming convention. Scroll up to the sandbox above and rename `useOnlineStatus` to `getOnlineStatus`. Notice that the linter won't allow you to call `useState`or`useEffect`inside of it anymore. Only Hooks and components can call other Hooks!
231
+
Eğer linter'ınız [React için yapılandırılmışsa,](/learn/editor-setup#linting)her zaman bu isimlendirme kuralını zorlayacaktır. Yukarıdaki sandbox'ta `useOnlineStatus`'u `getOnlineStatus` olarak yeniden adlandırın. Linter'ınızın artık onun içinde `useState`veya`useEffect`çağırmaya izin vermediğini fark edin. Sadece Hook'lar ve bileşenler diğer Hook'ları çağırabilir!
232
232
233
233
</Note>
234
234
235
235
<DeepDive>
236
236
237
-
#### Should all functions called during rendering start with the use prefix? {/*should-all-functions-called-during-rendering-start-with-the-use-prefix*/}
237
+
#### Render sırasında çağrılan tüm fonksiyonlar `use` ön eki ile mi başlamalıdır? {/*should-all-functions-called-during-rendering-start-with-the-use-prefix*/}
238
238
239
-
No. Functions that don't *call* Hooks don't need to *be* Hooks.
239
+
Hayır. Hook'ları *çağırmayan* fonksiyonlar Hook *olmak* zorunda değildir.
240
240
241
-
If your function doesn't call any Hooks, avoid the `use`prefix. Instead, write it as a regular function *without* the `use` prefix. For example, `useSorted`below doesn't call Hooks, so call it `getSorted`instead:
241
+
Eğer fonksiyonunuz herhangi bir Hook çağırmıyorsa, `use`ön eki kullanmayın. Bunun yerine onu `use` ön eki *bulunmayan* bir sıradan fonksiyon olarak yazın. Örneğin, aşağıdaki `useSorted` Hook çağırmadığından, onu `getSorted`olarak çağırın:
242
242
243
243
```js
244
-
// 🔴 Avoid: A Hook that doesn't use Hooks
244
+
// 🔴 Kaçının: Hook kullanmayan bir Hook
245
245
functionuseSorted(items) {
246
246
returnitems.slice().sort();
247
247
}
248
248
249
-
// ✅ Good: A regular function that doesn't use Hooks
249
+
// ✅ İyi: Hook kullanmayan bir sıradan fonksiyon
250
250
functiongetSorted(items) {
251
251
returnitems.slice().sort();
252
252
}
253
253
```
254
254
255
-
This ensures that your code can call this regular function anywhere, including conditions:
255
+
Bu, kodunuzun bu sıradan fonksiyonu, koşullar dahil olmak üzere herhangi bir yerde çağırabileceğinden emin olur:
256
256
257
257
```js
258
258
functionList({ items, shouldSort }) {
259
259
let displayedItems = items;
260
260
if (shouldSort) {
261
-
// ✅ It's ok to call getSorted() conditionally because it's not a Hook
261
+
// ✅ Koşullu olarak getSorted() çağırmak sorun değil çünkü o bir Hook değil
262
262
displayedItems =getSorted(items);
263
263
}
264
264
// ...
265
265
}
266
266
```
267
-
267
+
// TODO: Continue from here
268
268
You should give `use` prefix to a function (and thus make it a Hook) if it uses at least one Hook inside of it:
0 commit comments