# Reactivity API: Core
## wrap()
Wraps and returns any given values with reactive state objects.
### Type
```lua
function wrap(value: T): State
function wrap(value: ...unknown): ...State
type State = {
Value: T,
value: T
}
```
### Details
The state object has a single mutable field `.Value`.
Read operations to `.Value` are tracked and write operations can trigger
dependency updates and watchers.
### Example
```lua
local count = wrap(0)
print(count.Value) -- 0
count.Value += 1
print(count.Value) -- 1
```
-------------------------------------------------------------------
## derive()
Derives a new reactive state object from an existing state object.
### Type
```lua
function derive(
(from) -> T,
cleanup: (value: T) -> ()?
): State
function from(T | State): T
```
### Details
The derived state will have its value recalculated when any state it derives from is updated.
Takes a callback that is immediately run to determine what states are being referenced. Only states referenced in the immediate function scope can trigger updates.
The state object returned by this function is readonly.
Has an optional cleanup parameter which takes a function that is called with the old value any
time the derived state recalculates a value.
An optional utility function is passed as the first argument to the callback, if given a state, the value of the state will be returned (changes to this state still triggers updates unlike `unwrap`), if given a value the value is returned.
> ⚠️ The callback cannot yield.
### Example
```lua
local count = wrap(0)
local text = derive(function() return "Count: "..count.Value end)
print(text.Value) -- "Count: 0"
count.Value += 1
print(text.Value) -- "Count: 1"
```
```lua
local count = wrap(0)
local text = derive(function(from)
return "Count: "..from(count)
end)
```
A shorthand method for deriving states also exists, following example is equivalent to the above:
```lua
local count = wrap(0)
local text = "Count: "..count -- all binary operators are supported
```
-------------------------------------------------------------------
## foreach()
Derives a new state object from an existing state object.
Designed to work specifically with table states.
Also works with non state tables.
### Type
```lua
function foreach( -- number as first arg
i: number,
transform: (key: number) -> (KO, VO),
cleanup: (KO, VO) -> ()?
): Map
function foreach( -- table as first arg
table: Map,
transform: (key: KI, value: VI) -> (KO, VO),
cleanup: (KO, VO) -> ()?
): Map
function foreach( -- state as first arg
state: State