Skip to content

Instantly share code, notes, and snippets.

@ajcrites
Created March 18, 2019 22:51
Show Gist options
  • Save ajcrites/75963c4304939820bb0b15c61591292d to your computer and use it in GitHub Desktop.
Save ajcrites/75963c4304939820bb0b15c61591292d to your computer and use it in GitHub Desktop.
@@ -1,20 +1,25 @@
-import React, { useState, createContext } from 'react';
+import React, { useReducer, createContext, Dispatch } from 'react';
import { HexInput } from '~/HexInput';
import { RgbaInput } from '~/RgbaInput';
+import { colorReducer } from '~/colorReducer';
interface ColorContextProps {
hex: string;
rgba: number[];
+
+ dispatch: Dispatch<Action>;
};
const ColorContext = createContext<ColorContextProps>({} as any);
export const ColorToolApp = () => {
- const [hex, setHex] = useState('');
- const [rgba, setRgba] = useState(['', '', '', '']);
+ const [{ hex, rgba }, dispatch] = useReducer(colorReducer, {
+ hex: '',
+ rgba: [0, 0, 0, 0],
+ });
const state: ColorContextProps = {
- hex, rgba, setHex, setRgba,
+ hex, rgba, dispatch,
};
return (
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment