Skip to content

Instantly share code, notes, and snippets.

@ajcrites
Created March 18, 2019 23:11
Show Gist options
  • Save ajcrites/ec4dac5bcef3506df3bb3931038873d2 to your computer and use it in GitHub Desktop.
Save ajcrites/ec4dac5bcef3506df3bb3931038873d2 to your computer and use it in GitHub Desktop.
@@ -1,5 +1,5 @@
export const ColorInput = () => {
- const { hex, setHex, setRgba } = useContext(ColorToolContext);
+ const { hex, dispatch } = useContext(ColorToolContext);
const input = useRef(null);
const [show, setShow] = useState(true);
@@ -13,8 +13,7 @@ export const ColorInput = () => {
}, []);
const onChange = ({ target: { value } }) => {
- setHex(value);
- setRgba(value);
+ dispatch({ type: 'UPDATE_HEX', payload: value })
};
return show ? (
@@ -2,10 +2,9 @@ import React, { useContext } from 'react';
import { ColorToolContext } from '~/ColorToolAppContext';
export const HexInput = () => {
- const { hex, setHex, setRgba } = useContext(ColorToolContext);
+ const { hex, dispatch } = useContext(ColorToolContext);
const onChange = ({ target: { value } }) => {
- setHex(value);
- setRgba(value);
+ dispatch({ type: 'UPDATE_HEX', payload: value })
};
return (
@@ -1,18 +1,20 @@ import React, { useContext, useRef } from 'react';
import { ColorToolContext } from '~/ColorToolAppContext';
export const RgbaInput = () => {
const inputs = [useRef(null), useRef(null), useRef(null), useRef(null)];
+ const { rgba, dispatch } = useContext(ColorToolContext);
const onChange = () => {
const colorValues = inputs.map(({ current: { value } }) => value);
- setRgba(colorValues);
+ dispatch({ type: 'UPDATE_RGBA', payload: colorValues });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment