Skip to content

Commit

Permalink
Doc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarParra committed Nov 21, 2024
1 parent b71b57d commit 5253d36
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,28 @@ export const counterPlusTwo = $computed(() => counterPlusOne.value + 1);

Because `$computed` values return a signal, you can use them as you would use any other signal.

## Tracking objects and arrays

By default, for a signal to be reactive it needs to be reassigned. This can be cumbersome when dealing with objects
and arrays, as you would need to reassign the whole object or array to trigger the reactivity.

To improve that experience, you can set the `track` flag to true when creating the signal. This will make the signal
reactive to changes in the object or array properties.

> 📒 Think about this as using the `@track` decorator in LWC properties. It works the exact same way behind the scenes.
```javascript
import { $signal } from "c/signals";

const obj = $signal({ x: 1, y: 2 }, { track: true });
const computedFromObj = $computed(() => obj.value.x + obj.value.y);

// When a value in the object changes, the computed value will automatically update
obj.value.x = 2;

console.log(computedFromObj.value); // 4
```

## Reacting to multiple signals

You can also use multiple signals in a single `computed` and react to changes in any of them.
Expand Down
4 changes: 2 additions & 2 deletions examples/demo-signals/lwc/demoSignals/shopping-cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import updateShoppingCart from "@salesforce/apex/ShoppingCartController.updateSh
*/

// Store each state change in the cart history
export const cartHistory = $signal([], {track: true});
export const cartHistory = $signal([], { track: true });
$effect(() => {
console.log('cartHistory', JSON.stringify(cartHistory.value, null, 2));
console.log("cartHistory", JSON.stringify(cartHistory.value, null, 2));
});
let isUndoing = false;

Expand Down
2 changes: 1 addition & 1 deletion src/lwc/signals/__tests__/computed.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { $computed, $signal, $effect } from "../core";
import { $computed, $signal } from "../core";

describe("computed values", () => {
test("can be created from a source signal", () => {
Expand Down

0 comments on commit 5253d36

Please sign in to comment.