preact/hooks#useReducer JavaScript Examples

The following examples show how to use preact/hooks#useReducer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CodeSettings.js    From v8-deopt-viewer with MIT License 6 votes vote down vote up
/**
 * @returns {[CodeSettingsState, (setting: keyof CodeSettingsState) => void]}
 */
export function useCodeSettingsState() {
	return useReducer((state, settingToToggle) => {
		return {
			...state,
			[settingToToggle]: !state[settingToToggle]
		};
	}, initialState);
}
Example #2
Source File: appState.js    From v8-deopt-viewer with MIT License 6 votes vote down vote up
/**
 * @typedef AppProviderProps
 * @property {import('preact').JSX.Element | import('preact').JSX.Element[]} children
 * @param {AppProviderProps} props
 */
export function AppProvider(props) {
	const [state, dispatch] = useReducer(appContextReducer, props, initialState);
	const dispatchers = useMemo(
		() => ({
			setSelectedPosition(newPosition) {
				dispatch({ type: "SET_SELECTED_POSITION", newPosition });
			},
			setSelectedEntry(entry) {
				dispatch({ type: "SET_SELECTED_ENTRY", entry });
			},
		}),
		[dispatch]
	);

	return (
		<AppDispatchContext.Provider value={dispatchers}>
			<AppStateContext.Provider value={state}>
				{props.children}
			</AppStateContext.Provider>
		</AppDispatchContext.Provider>
	);
}