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

24 lines
548 B
TypeScript

import { useEffect, useState } from "react"
import type { Presenter } from "@/presentation/presenters/_Presenter"
export const usePresenterState = <State>(
presenter: Presenter<State>,
): State => {
const [state, setState] = useState<State>(presenter.initialState)
useEffect(() => {
const presenterSubscription = (state: State): void => {
setState(state)
}
presenter.subscribe(presenterSubscription)
return () => {
return presenter.unsubscribe(presenterSubscription)
}
}, [presenter])
return state
}