Set initial state for provider; fix useReducer usage

This commit is contained in:
dannyrb 2020-06-26 13:29:10 -04:00
parent 077143bd4a
commit aa4e88c2fa

View File

@ -7,7 +7,14 @@ import React, {
} from 'react'; } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
export const ViewportGridContext = createContext(); const DEFAULT_STATE = {
numRows: 1,
numCols: 1,
viewports: [],
activeViewportIndex: 0,
};
export const ViewportGridContext = createContext(DEFAULT_STATE);
// export function ViewportGridProvider({ reducer, initialState, children }) { // export function ViewportGridProvider({ reducer, initialState, children }) {
// return ( // return (
@ -18,13 +25,6 @@ export const ViewportGridContext = createContext();
// } // }
export function ViewportGridProvider({ children, service }) { export function ViewportGridProvider({ children, service }) {
const DEFAULT_STATE = {
numRows: 1,
numCols: 1,
viewports: [],
activeViewportIndex: 0,
};
const viewportGridReducer = (state, action) => { const viewportGridReducer = (state, action) => {
switch (action.type) { switch (action.type) {
case 'SET_ACTIVE_VIEWPORT_INDEX': case 'SET_ACTIVE_VIEWPORT_INDEX':
@ -62,8 +62,8 @@ export function ViewportGridProvider({ children, service }) {
}; };
const [viewportGridState, dispatch] = useReducer( const [viewportGridState, dispatch] = useReducer(
viewportGridReducer,
DEFAULT_STATE, DEFAULT_STATE,
viewportGridReducer
); );
const getState = useCallback(() => viewportGridState, [viewportGridState]); const getState = useCallback(() => viewportGridState, [viewportGridState]);