hedgedoc/src/hooks/common/use-application-state.ts
Tilman Vatteroth 829cc2fe48
Add application state hook (#1308)
* Add application state hook

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>

* Add docs

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
2021-06-11 15:21:24 +02:00

22 lines
850 B
TypeScript

/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { useSelector } from 'react-redux'
import { ApplicationState } from '../../redux'
import equal from 'fast-deep-equal'
/**
* Accesses the global application state to retrieve information.
*
* @param selector A selector function that extracts the needed information from the state.
* @param checkForEquality An optional custom equality function. If not provided then {@link equal equal from fast-deep-equal} will be used.
*/
export const useApplicationState = <TSelected>(
selector: (state: ApplicationState) => TSelected,
checkForEquality?: (a: TSelected, b: TSelected) => boolean
): TSelected => {
return useSelector<ApplicationState, TSelected>(selector, checkForEquality ? checkForEquality : equal)
}