Skip to content

Instantly share code, notes, and snippets.

import React, { useState, createContext } from 'react';
import { HexInput } from '~/HexInput';
import { RgbaInput } from '~/RgbaInput';
interface ColorContextProps {
hex: string;
rgba: number[];
setHex: (value) => void;
@@ -9,8 +9,8 @@ export const ColorToolApp = () => {
return (
<main>
- <HexInput hex={hex} setHex={setHex} />
- <RgbaInput rgba={rgba} setRgba={setRgba} />
+ <HexInput hex={hex} setHex={setHex} setRgba={setRgba} />
+ <RgbaInput rgba={rgba} setRgba={setRgba} setHex={setHex} />
</main>
);
@@ -1,20 +1,19 @@
-import React from 'react';
+import React, { useRef } from 'react';
export const RgbaInput = ({ rgba, setRgba }) => {
- const onChange = idx => ({ target: { value } }) => {
- const newRgba = [...rgba];
- newRgba[idx] = value;
+ const inputs = [useRef(null), useRef(null), useRef(null), useRef(null)];
+ const onChange = () => {
import React from 'react';
export const HexInput = ({ hex, setHex }) => {
const onChange = ({ target: { value } }) => {
setHex(value);
};
return (
<label>
Hex:
import React, { useState } from 'react';
import { HexInput } from '~/HexInput';
import { RgbaInput } from '~/RgbaInput';
export const ColorToolApp = () => {
const [hex, setHex] = useState('');
const [rgba, setRgba] = useState([0, 0, 0, 0]);
return (
import React from 'react';
export const HexInput = () => (
<label>
Hex:
<input />
</label>
);
# this also works in vim
rename 's/-(\w)/\U$1/g' $FILES
export const TimesTableApp = () => {
const [pointCount, setPointCount] = useState(10);
const [timesTable, setTimesTable] = useState(2);
const [lineColor, setLineColor] = useState('#000000');
const state: TimesTableContextProps = {
pointCount,
timesTable,
lineColor,
export const TimesTable = () => {
const { timesTable, pointCount, lineColor } = useContext(TimesTableContext);
const props = { timesTable, pointCount, lineColor };
...
};
export class TimesTableApp extends React.Component {
changeValue = valueName => value => {
if (isFunction(value)) {
this.setState({ [valueName]: value(this.state[valueName]) });
else {
this.setState({ [valueName]: value });
}
};
...