# Lire, modifier et surveiller l'état

> Utilisez SET, GET, UPD, PUSH et WATCH pour travailler avec l'état global et réagir quand un chemin change.

Framework: jcomponent20
Product: jComponent 20 (jcomponent@20)
Language: fr
Content-SHA256: 6d2e750bfc942de887d2987ac45abb466d5d6492d019c83a6504b34996b0c967
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: jcomponent20@3fedd009a277f50fddc608b97afbec553b67c51b
Review: not-recorded

L'état est un objet global adressé par des **chemins** comme `cart.items`. Les fonctions pour le manipuler :

- `SET(path, value)` écrit une valeur et notifie tout ce qui est lié au chemin
- `GET(path)` lit une valeur
- `UPD(path)` notifie le chemin après que vous avez modifié l'objet sur place (`GET('cart').discount = 5; UPD('cart')`)
- `PUSH(path, value)` ajoute à un tableau et notifie ; avec un `^` devant le chemin, la valeur est ajoutée au début
- `WATCH(path, callback)` exécute le callback avec la nouvelle valeur chaque fois que le chemin change ; joignez plusieurs chemins avec `+`

```html public/index.html
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>jComponent</title>
	<link rel="stylesheet" href="//cdn.componentator.com/spa.min@20.css" />
	<script src="//cdn.componentator.com/spa.min@20.js"></script>
</head>
<body>
	<p><ui-bind path="cart.items" config="text:value ? value.length + ' items' : 'empty'"></ui-bind></p>

	<script>

		var log = [];

		WATCH('cart.items', function(value) {
			log.push(value.length);
		});

		SET('cart.items', []);
		PUSH('cart.items', 'book');
		PUSH('^cart.items', 'pen');

		GET('cart.items').push('lamp');
		UPD('cart.items');

	</script>

</body>
</html>
```
