Skip to content
Merged
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
32daa5d
docs(usememo): intro
annisann Apr 28, 2023
cf480bc
docs(usememo): reference
annisann Apr 28, 2023
8bae2e9
Merge branch 'main' into react-hooks--usememo
mhaidarhanif Apr 28, 2023
88d4f93
Merge branch 'main' into react-hooks--usememo
mhaidarhanif Apr 29, 2023
219766d
Merge branch 'reactjs:main' into react-hooks--usememo
annisann Apr 29, 2023
4aa7430
Merge branch 'main' into react-hooks--usememo
annisann May 2, 2023
a532cb3
docs(usememo): apply suggestion on #usage
annisann May 2, 2023
b49da8b
docs(usememo): translates headings `returns` and `caveats` as referen…
annisann May 2, 2023
5653fb0
docs(usememo): translate `useMemo` usage task one (#406)
annisann May 2, 2023
1d13042
Merge branch 'react-hooks--usememo' of https://github.com/annisann/id…
annisann May 2, 2023
ce41ede
docs(usememo): translate skipped parts on useMemo usage task one. (#406)
annisann May 2, 2023
f85f4df
docs(usememo): translation of `useMemo` usage task two. (#406)
annisann May 2, 2023
d004e49
docs(usememo): uniform `filterTodos` console log
annisann May 2, 2023
023f13b
docs(usememo): translation on `useMemo` usage task three. (#406)
annisann May 2, 2023
86acfb1
docs(usememo): translation on `useMemo` usage task four. (#406)
annisann May 2, 2023
77ab8b6
docs(usememo): apply "rendering" translation suggestion based on disc…
annisann May 2, 2023
a0a9c7b
Merge branch 'main' into react-hooks--usememo
annisann May 3, 2023
9a6fbb1
docs(usememo): troubleshooting task one. (#406)
annisann May 3, 2023
86b5918
Merge branch 'react-hooks--usememo' of https://github.com/annisann/id…
annisann May 3, 2023
6fa0ba9
docs(usememo): troubleshooting task two. (#406)
annisann May 3, 2023
3c6f833
docs(usememo): troubleshooting task 3 of 4. (#406)
annisann May 3, 2023
e21897e
docs(usememo): troubleshooting task 4/4. (#406)
annisann May 3, 2023
d18d26b
Merge branch 'main' into react-hooks--usememo
annisann May 3, 2023
d32effa
docs(usememo): apply suggestions from reviewer
annisann May 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/content/reference/react/useMemo.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: useMemo

<Intro>

`useMemo` is a React Hook that lets you cache the result of a calculation between re-renders.
`useMemo` merupakan React Hook yang memungkinkan kamu untuk meng-*cache* hasil perhitungan pada tiap *render*.

```js
const cachedValue = useMemo(calculateValue, dependencies)
Expand All @@ -16,11 +16,11 @@ const cachedValue = useMemo(calculateValue, dependencies)

---

## Reference {/*reference*/}
## Referensi {/*reference*/}

### `useMemo(calculateValue, dependencies)` {/*usememo*/}

Call `useMemo` at the top level of your component to cache a calculation between re-renders:
Panggil `useMemo` di tingkat atas komponen Anda untuk meng-*cache* hasil perhitungan pada tiap *render.

```js
import { useMemo } from 'react';
Expand All @@ -34,29 +34,29 @@ function TodoList({ todos, tab }) {
}
```

[See more examples below.](#usage)
[Lihat contoh lainnya.](#usage)

#### Parameters {/*parameters*/}

* `calculateValue`: The function calculating the value that you want to cache. It should be pure, should take no arguments, and should return a value of any type. React will call your function during the initial render. On next renders, React will return the same value again if the `dependencies` have not changed since the last render. Otherwise, it will call `calculateValue`, return its result, and store it so it can be reused later.
* `calculateValue`: Fungsi untuk menghitung nilai yang ingin Anda *cache*. Nilai harus murni, tidak mempunyai argumen, dan harus membalikkan nilai dari tipe data apapun. React akan memanggil fungsi tersebut pada *render* awal. Pada *render* selanjutnya, React akan mengembalikan nilai yang sama jika `dependencies` tidak berubah dari *render* terakhir. Sebaliknya, React akan memanggil `calculateValue`, mengembalikan hasil nilainya, dan disimpan sehingga nilai tersebut dapat digunakan kembali nantinya.

* `dependencies`: The list of all reactive values referenced inside of the `calculateValue` code. Reactive values include props, state, and all the variables and functions declared directly inside your component body. If your linter is [configured for React](/learn/editor-setup#linting), it will verify that every reactive value is correctly specified as a dependency. The list of dependencies must have a constant number of items and be written inline like `[dep1, dep2, dep3]`. React will compare each dependency with its previous value using the [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) comparison.
* `dependencies`: Daftar dari semua nilai reaktif yang direferensikan dalam kode `calculatedValue`. Nilai reaktif termasuk *props*, *state*, serta seluruh variabel dan fungsi yang dideklarasi langsung dalam badan komponen. Jika *linter* Anda sudah [dikonfigurasi untuk React](/learn/editor-setup#linting), maka *linter* akan memeriksa setiap nilai reaktif sudah dispesifikasikan sebagai sebuah *dependency* dengan benar. Daftar `dependency` harus memiliki jumlah *item* yang tetap dan dituliskan dalam sebaris seperti `[dep1, dep2, dep3]`. React akan membandingkan tiap `dependency` dengan nilai sebelumnya menggunakan perbandingan [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is).

#### Returns {/*returns*/}

On the initial render, `useMemo` returns the result of calling `calculateValue` with no arguments.
Pada *render* pertama kali, `useMemo` mengembalikan hasil pemanggilan `calculateValue` tanpa argumen.

During next renders, it will either return an already stored value from the last render (if the dependencies haven't changed), or call `calculateValue` again, and return the result that `calculateValue` has returned.
Saat *render* selanjutnya, akan mengembalikan nilai yang telah disimpan dari *render* selanjutnya (apabila `dependency` belum berubah), atau kembali memanggil `calculateValue`, dan mengembalikan hasil yang dikembalikan oleh `calculateValue`.

#### Caveats {/*caveats*/}

* `useMemo` is a Hook, so you can only call it **at the top level of your component** or your own Hooks. You can't call it inside loops or conditions. If you need that, extract a new component and move the state into it.
* In Strict Mode, React will **call your calculation function twice** in order to [help you find accidental impurities.](#my-calculation-runs-twice-on-every-re-render) This is development-only behavior and does not affect production. If your calculation function is pure (as it should be), this should not affect your logic. The result from one of the calls will be ignored.
* React **will not throw away the cached value unless there is a specific reason to do that.** For example, in development, React throws away the cache when you edit the file of your component. Both in development and in production, React will throw away the cache if your component suspends during the initial mount. In the future, React may add more features that take advantage of throwing away the cache--for example, if React adds built-in support for virtualized lists in the future, it would make sense to throw away the cache for items that scroll out of the virtualized table viewport. This should be fine if you rely on `useMemo` solely as a performance optimization. Otherwise, a [state variable](/reference/react/useState#avoiding-recreating-the-initial-state) or a [ref](/reference/react/useRef#avoiding-recreating-the-ref-contents) may be more appropriate.
* `useMemo` merupakan Hook, jadi Anda hanya dapat memanggilnya **di tingkat atas komponen Anda** atau pada Hook Anda sendiri. Anda tidak bisa memanggilnya di dalam perulangan atau suatu kondisi. Jika Anda memerlukannya, ekstrak komponen baru dan pindahkan *state* tersebut ke dalamnya.
* Pada *Strict Mode*, React akan **memanggil fungsi perhitungan Anda dua kali** untuk [membantu Anda menemukan ketidakmurnian (*impurity*) yang tidak disengaja.](#my-calculation-runs-twice-on-every-re-render) Perlakuan ini hanya terjadi pada *development* dan tidak memengaruhi *production*. Jika fungsi perhitungan Anda murni (sebagaimana mestinya), maka seharusnya tidak memengaruhi logika Anda. Hasil dari salah satu pemanggilan akan diabaikan.
* React **tidak akan membuang nilai *cache* kecuali ada alasan spesifik untuk melakukannya**. Sebagai contoh, pada *development*, React membuang *cache* ketika Anda menyunting *file* komponen Anda. Baik dalam *development* dan *production*, React akan membuang *cache* jika komponen Anda menunda saat *mount* awal. Kedepannya, React mungkin akan menambahkan fitur yang memanfaatkan pembuangan *cache*--misalnya, jika React menambahkan dukungan bawaan untuk daftar virtual di masa depan, maka akan masuk akal untuk membuang *cache* untuk *item* yang keluar dari tampilan tabel virtual. Hal ini seharusnya tidak masalah jika Anda hanya mengandalkan `useMemo` sebagai optimasi kinerja semata. Jika tidak, [variabel status](/reference/react/useState#avoiding-recreating-the-initial-state) atau sebuah [ref](/reference/react/useRef#avoiding-recreating-the-ref-contents) mungkin lebih tepat.

<Note>

Caching return values like this is also known as [*memoization*,](https://en.wikipedia.org/wiki/Memoization) which is why this Hook is called `useMemo`.
Meng-*cache* hasil kembalian seperti ini juga dikenal sebagai [*memoisasi*](https://en.wikipedia.org/wiki/Memoization), maka dari itu Hook ini disebut sebagai `useMemo`.

</Note>

Expand Down