1
1
mirror of https://github.com/theoludwig/p61-project.git synced 2024-07-17 07:00:12 +02:00
p61-project/hooks/usePresenterState.ts

22 lines
530 B
TypeScript
Raw Normal View History

2024-03-15 22:48:28 +01:00
import { useEffect, useState } from "react"
import type { Presenter } from "@/data/infrastructure/presenters/_Presenter"
export const usePresenterState = <S>(presenter: Presenter<S>): S => {
const [state, setState] = useState<S>(presenter.initialState)
useEffect(() => {
const presenterSubscription = (state: S): void => {
setState(state)
}
presenter.subscribe(presenterSubscription)
return () => {
return presenter.unsubscribe(presenterSubscription)
}
}, [presenter])
return state
}