Browse Source

feat: react-track 替代useContext & useReducer

一个是通过memo优化性能比较麻烦,第二是memo比较object做不到deep diff
master
suanmei 5 years ago
committed by 拾邑
parent
commit
05c785a4b7
  1. 20
      packages/json-schema-editor/src/context/ui.tsx
  2. 16
      packages/json-schema-editor/src/reducer/ui.ts

20
packages/json-schema-editor/src/context/ui.tsx

@ -1,20 +0,0 @@
import * as React from 'react';
import { createContext, useContext, useReducer } from 'react';
import uiReducer from '../reducer/ui';
import { UIContext } from '../model';
interface ProviderProps {
children: React.ReactChild;
}
const initialData = { formVisible: true, aaa: true };
const Context = createContext({ uiState: initialData } as UIContext);
export const UIProvider = ({ children }: ProviderProps): JSX.Element => {
const [uiState, uiDispatch] = useReducer(uiReducer, initialData);
return <Context.Provider value={{ uiState, uiDispatch }}>{children}</Context.Provider>;
};
export const useUI = (): UIContext => useContext(Context);

16
packages/json-schema-editor/src/reducer/ui.ts

@ -1,16 +0,0 @@
import { UIState, UIAction } from '../model';
const reducer = (state: UIState, action: UIAction): UIState => {
switch (action.type) {
case 'TOGGLE_FORM_VISIBLE':
return { formVisible: state.formVisible, aaa: false };
case 'increment':
return state;
case 'decrement':
return state;
default:
return state;
}
};
export default reducer;
Loading…
Cancel
Save