From 317ca2a6e898ce2833a1636f499e1441f7ad5e3b Mon Sep 17 00:00:00 2001 From: enesakkose Date: Thu, 11 May 2023 13:46:54 +0300 Subject: [PATCH 1/2] translate updating-objects-in-state --- .../learn/updating-objects-in-state.md | 242 +++++++++--------- 1 file changed, 120 insertions(+), 122 deletions(-) diff --git a/src/content/learn/updating-objects-in-state.md b/src/content/learn/updating-objects-in-state.md index 1e23c8d3d..06154a2aa 100644 --- a/src/content/learn/updating-objects-in-state.md +++ b/src/content/learn/updating-objects-in-state.md @@ -1,57 +1,55 @@ --- -title: Updating Objects in State +title: State içerisindeki nesneleri güncelleme --- -State can hold any kind of JavaScript value, including objects. But you shouldn't change objects that you hold in the React state directly. Instead, when you want to update an object, you need to create a new one (or make a copy of an existing one), and then set the state to use that copy. +State, nesneler dahil olmak üzere herhangi bir JavaScript değerini tutabilir. Ancak React state içerisinde tuttuğunuz nesneleri direkt olarak değiştirmemelisiniz. Bunun yerine bir nesneyi güncellemek istediğinizde, yeni bir nesne oluşturmanız gerekmektedir (veya varolan bir nesnenin kopyasını oluşturmalısınız) daha sonra state'i kopyaladığınız nesneyi kullanması için ayarlamalısınız. -- How to correctly update an object in React state -- How to update a nested object without mutating it -- What immutability is, and how not to break it -- How to make object copying less repetitive with Immer +- React state'i içerisinde bir nesneyi doğru şekilde güncelleyebileceksiniz. +- İç içe bir nesneyi mutasyona uğratmadan güncelleyebileceksiniz. +- Değişmezlik nedir, ve onu nasıl bozmadan sürdürebileceğinizi. +- Immer ile nesne kopyalamayı daha kolay şekilde yapabileceksiniz. -## What's a mutation? {/*whats-a-mutation*/} - -You can store any kind of JavaScript value in state. +## Mutasyon nedir? {/*whats-a-mutation*/} +State içerisinde herhangi bir JavaScript değerini tutabilirsiniz. ```js const [x, setX] = useState(0); ``` -So far you've been working with numbers, strings, and booleans. These kinds of JavaScript values are "immutable", meaning unchangeable or "read-only". You can trigger a re-render to _replace_ a value: +Şimdiye kadar sayılarla, stringlerle ve booleanlarla çalıştınız. Bu JavaScript değerleri "değişmez" veya "salt okunur" anlamına gelir. +Bir değeri _değiştirmek_ için yeniden render işlemi yapabilirsiniz. ```js setX(5); ``` +`x` state'i `0`'ken `5` ile değiştirildi, ama _`0` sayısının kendisi_ değişmedi. JavaScript'te, sayılar, stringler ve booleanlar gibi yerleşik temel veri tiplerinde herhangi bir değişiklik yapmak mümkün değildir. -The `x` state changed from `0` to `5`, but the _number `0` itself_ did not change. It's not possible to make any changes to the built-in primitive values like numbers, strings, and booleans in JavaScript. - -Now consider an object in state: +Şimdi state içerisinde bir nesne düşünün: ```js const [position, setPosition] = useState({ x: 0, y: 0 }); ``` - -Technically, it is possible to change the contents of _the object itself_. **This is called a mutation:** +Teknik olarak, _nesnenin kendisinin_ içeriğini değiştirmek mümkündür. **Buna mutasyon denir:** ```js position.x = 5; ``` -However, although objects in React state are technically mutable, you should treat them **as if** they were immutable--like numbers, booleans, and strings. Instead of mutating them, you should always replace them. +Ancak, React state içerisindeki nesneler teknik olarak değiştirilebilir olsalar da, sayılar, booleans ve dizeler **sözde** değişmezmiş gibi muamele edilmelidir. Onları mutasyona uğratmak yerine, her zaman yenilerini oluşturmalısınız. -## Treat state as read-only {/*treat-state-as-read-only*/} +## State'i salt okunur olarak ele alın {/*treat-state-as-read-only*/} -In other words, you should **treat any JavaScript object that you put into state as read-only.** +Başka bir deyişle, **State içerisine koyduğunuz herhangi bir JavaScript nesnesini salt okunur olarak ele almalısınız.** -This example holds an object in state to represent the current pointer position. The red dot is supposed to move when you touch or move the cursor over the preview area. But the dot stays in the initial position: +Bu örnek, mevcut imlec pozisyonunu temsil eden bir nesneyi state içerisinde tutar. Kırmızı nokta, siz önizleme alanına dokunduğunuzda veya imleci üzerinde hareket ettirdiğinizde hareket etmesi gerekir. Ancak nokta başlangıç pozisyonunda kalıyor. @@ -94,7 +92,7 @@ body { margin: 0; padding: 0; height: 250px; } -The problem is with this bit of code. +Problem bu kod parçacığıyla ilgili. ```js onPointerMove={e => { @@ -103,9 +101,9 @@ onPointerMove={e => { }} ``` -This code modifies the object assigned to `position` from [the previous render.](/learn/state-as-a-snapshot#rendering-takes-a-snapshot-in-time) But without using the state setting function, React has no idea that object has changed. So React does not do anything in response. It's like trying to change the order after you've already eaten the meal. While mutating state can work in some cases, we don't recommend it. You should treat the state value you have access to in a render as read-only. +Bu kod, [önceki render](/learn/state-as-a-snapshot#rendering-takes-a-snapshot-in-time) işleminden `position` değişkenine atanmış nesneyi değiştirir. Ancak state ayarlama fonksiyonunu kullanmadan, React bu nesnenin değiştiğini bilmez. Bu nedenle, React herhangi bir tepki vermez. Bu, yemeği yedikten sonra siparişin değiştirilmeye çalışılması gibi bir şeydir. State'in mutasyona uğratılması bazı durumlarda çalışabilir, ancak önermiyoruz. Render işleminde erişebildiğiniz state değerini salt okunur olarak ele almanız gerekir. -To actually [trigger a re-render](/learn/state-as-a-snapshot#setting-state-triggers-renders) in this case, **create a *new* object and pass it to the state setting function:** +Bu durumda, gerçekten [yeniden render işlemini tetiklemek](/learn/state-as-a-snapshot#setting-state-triggers-renders) için, ***yeni* bir nesne oluşturun ve onu state ayarlama fonksiyonuna geçirin:** ```js onPointerMove={e => { @@ -116,12 +114,12 @@ onPointerMove={e => { }} ``` -With `setPosition`, you're telling React: +`setPosition` ile, React'a şunu söylüyorsunuz: -* Replace `position` with this new object -* And render this component again +* Bu yeni nesne ile `position`'ı değiştir +* Ve bu bileşeni tekrar render et -Notice how the red dot now follows your pointer when you touch or hover over the preview area: +Dikkat edin, kırmızı nokta şimdi önizleme alanına dokunduğunuzda veya üzerine geldiğinizde imlecinizi takip ediyor: @@ -168,16 +166,16 @@ body { margin: 0; padding: 0; height: 250px; } -#### Local mutation is fine {/*local-mutation-is-fine*/} +#### Yerel mutasyon sorun değildir {/*local-mutation-is-fine*/} -Code like this is a problem because it modifies an *existing* object in state: +Bu şekildeki kod, state içerisinde *varolan* bir nesneyi değiştirdiği için bir problemdir. ```js position.x = e.clientX; position.y = e.clientY; ``` -But code like this is **absolutely fine** because you're mutating a fresh object you have *just created*: +Ancak bu şekildeki kod **kesinlikle sorunsuzdur** çünkü *yeni oluşturduğunuz* bir nesneyi değiştiriyorsunuz; ```js const nextPosition = {}; @@ -186,7 +184,7 @@ nextPosition.y = e.clientY; setPosition(nextPosition); ```` -In fact, it is completely equivalent to writing this: +Aslında, bunu yazmakla tamamen aynı anlama geliyor: ```js setPosition({ @@ -195,15 +193,15 @@ setPosition({ }); ``` -Mutation is only a problem when you change *existing* objects that are already in state. Mutating an object you've just created is okay because *no other code references it yet.* Changing it isn't going to accidentally impact something that depends on it. This is called a "local mutation". You can even do local mutation [while rendering.](/learn/keeping-components-pure#local-mutation-your-components-little-secret) Very convenient and completely okay! +Mutasyon sadece state içerisinde zaten *mevcut olan* nesneleri değiştirdiğinizde bir problem oluşturur. Yeni oluşturduğunuz bir nesneyi değiştirmek *bu nesneye henüz başka bir kod referans vermediği* için tamamen sorunsuzdur. Nesneyi değiştirmek, nesneye bağlı olan bir şeyi yanlışlıkla etkileme olasılığını ortadan kaldıracaktır. Buna "yerel mutasyon" denir. -## Copying objects with the spread syntax {/*copying-objects-with-the-spread-syntax*/} +## Spread sözdizimi ile nesnelerin kopyalanması {/*copying-objects-with-the-spread-syntax*/} -In the previous example, the `position` object is always created fresh from the current cursor position. But often, you will want to include *existing* data as a part of the new object you're creating. For example, you may want to update *only one* field in a form, but keep the previous values for all other fields. +Önceki örnekte, `position` nesnesi her zaman mevcut imlec konumuna göre yeniden oluşturulur. Ama çoğu zaman, yeni oluşturduğunuz nesnenin bir parçası olarak *mevcut* verileri de dahil etmek isteyebilirsiniz. Örneğin, bir formda *sadece tek* bir alanı güncellemek ve diğer form alanlarının önceki değerlerini korumak isteyebilirsiniz -These input fields don't work because the `onChange` handlers mutate the state: +Bu input alanları, `onChange` yöneticileri state'in mutate olmasına neden oldukları için çalışmazlar: @@ -232,14 +230,14 @@ export default function Form() { return ( <>