Skip to content

Commit fbc29d3

Browse files
committed
📝 add doc entries for useTarget and useTargets
1 parent 1ec0a53 commit fbc29d3

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,44 @@ useTraitEffect(world, GameState, (state) => {
10381038
})
10391039
```
10401040
1041+
### `useTarget`
1042+
1043+
Observes an entity, or world, for a relation and reactively returns the first target entity. Returns `undefined` if no target exists.
1044+
1045+
```js
1046+
const ChildOf = relation()
1047+
1048+
function ParentDisplay({ entity }) {
1049+
// Returns the first target of the ChildOf relation
1050+
const parent = useTarget(entity, ChildOf)
1051+
1052+
if (!parent) return <div>No parent</div>
1053+
1054+
return <div>Parent: {parent.id()}</div>
1055+
}
1056+
```
1057+
1058+
### `useTargets`
1059+
1060+
Observes an entity, or world, for a relation and reactively returns all target entities as an array. Returns an empty array if no targets exist.
1061+
1062+
```js
1063+
const Contains = relation()
1064+
1065+
function InventoryDisplay({ entity }) {
1066+
// Returns all targets of the Contains relation
1067+
const items = useTargets(entity, Contains)
1068+
1069+
return (
1070+
<ul>
1071+
{items.map((item) => (
1072+
<li key={item.id()}>Item {item.id()}</li>
1073+
))}
1074+
</ul>
1075+
)
1076+
}
1077+
```
1078+
10411079
### `useActions`
10421080
10431081
Returns actions bound to the world that is in context. Use actions created by `defineActions`.

packages/publish/README.md

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,15 @@ createRoot(document.getElementById('root')!).render(
7272
function RocketRenderer() {
7373
// Reactively update whenever the query updates with new entities
7474
const rockets = useQuery(Position, Velocity)
75-
return (
76-
<>
77-
{rockets.map((entity) => <RocketView key={entity} entity={entity} />)}
78-
</>
79-
)
75+
return rockets.map((entity) => <RocketView key={entity} entity={entity} />)
8076
}
8177

8278
function RocketView({ entity }) {
8379
// Observes this entity's position trait and reactively updates when it changes
8480
const position = useTrait(entity, Position)
8581
return (
8682
<div style={{ position: 'absolute', left: position.x ?? 0, top: position.y ?? 0 }}>
87-
🚀
83+
🚀
8884
</div>
8985
)
9086
}
@@ -1042,6 +1038,44 @@ useTraitEffect(world, GameState, (state) => {
10421038
})
10431039
```
10441040
1041+
### `useTarget`
1042+
1043+
Observes an entity, or world, for a relation and reactively returns the first target entity. Returns `undefined` if no target exists.
1044+
1045+
```js
1046+
const ChildOf = relation()
1047+
1048+
function ParentDisplay({ entity }) {
1049+
// Returns the first target of the ChildOf relation
1050+
const parent = useTarget(entity, ChildOf)
1051+
1052+
if (!parent) return <div>No parent</div>
1053+
1054+
return <div>Parent: {parent.id()}</div>
1055+
}
1056+
```
1057+
1058+
### `useTargets`
1059+
1060+
Observes an entity, or world, for a relation and reactively returns all target entities as an array. Returns an empty array if no targets exist.
1061+
1062+
```js
1063+
const Contains = relation()
1064+
1065+
function InventoryDisplay({ entity }) {
1066+
// Returns all targets of the Contains relation
1067+
const items = useTargets(entity, Contains)
1068+
1069+
return (
1070+
<ul>
1071+
{items.map((item) => (
1072+
<li key={item.id()}>Item {item.id()}</li>
1073+
))}
1074+
</ul>
1075+
)
1076+
}
1077+
```
1078+
10451079
### `useActions`
10461080
10471081
Returns actions bound to the world that is in context. Use actions created by `defineActions`.

0 commit comments

Comments
 (0)