mirror of
https://github.com/morten-olsen/shipped.git
synced 2026-02-07 23:26:23 +01:00
init
This commit is contained in:
41
packages/fleet-map/src/bridge/hooks.ts
Normal file
41
packages/fleet-map/src/bridge/hooks.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { useContext, useEffect, useState } from "react"
|
||||
import { BridgeContext } from "./context"
|
||||
|
||||
const useBridgeState = () => {
|
||||
const { bridge } = useContext(BridgeContext);
|
||||
const [state, setState] = useState(bridge?.state);
|
||||
useEffect(() => {
|
||||
if (!bridge) return;
|
||||
const update = () => {
|
||||
setState(bridge.state);
|
||||
}
|
||||
bridge.on('update', update);
|
||||
return () => {
|
||||
bridge.off('update', update);
|
||||
};
|
||||
}, [bridge]);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
const useBridgeWorld = () => {
|
||||
const { bridge } = useContext(BridgeContext);
|
||||
const [state, setState] = useState(bridge?.world);
|
||||
useEffect(() => {
|
||||
if (!bridge) return;
|
||||
const update = () => {
|
||||
setState(bridge.world);
|
||||
}
|
||||
bridge.on('update', update);
|
||||
return () => {
|
||||
bridge.off('update', update);
|
||||
};
|
||||
}, [bridge]);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
export {
|
||||
useBridgeState,
|
||||
useBridgeWorld,
|
||||
};
|
||||
Reference in New Issue
Block a user