22 lines
523 B
TypeScript
22 lines
523 B
TypeScript
import { useEffect, useState } from "react"
|
|
|
|
import type { Presenter } from "@/presentation/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
|
|
}
|