Skip to content

Instantly share code, notes, and snippets.

@rphansen91
Created June 14, 2020 19:43
Show Gist options
  • Save rphansen91/65f3c9374704eae50b904fd1040129d4 to your computer and use it in GitHub Desktop.
Save rphansen91/65f3c9374704eae50b904fd1040129d4 to your computer and use it in GitHub Desktop.
HawkCounter.tsx
import React from 'react';
import { hawk, hawkeye, useHawkState, useHawkSetState } from 'react-hawk';
import Counter from '../Components/Counter'
const counterState = hawk({
key: 'counter',
default: 0
});
const isEvenState = hawkeye({
key: 'isEven',
get: ({ get }) => {
const count = get(counterState)
return count % 2 ? 'odd' : 'even'
}
})
const useIncrease = () => {
const setCounter = useHawkSetState(counterState)
const increase = (n = 1) => {
setCounter(count => count + n)
}
return increase
}
const useDecrease = () => {
const setCounter = useHawkSetState(counterState)
const decrease = (n = 1) => {
setCounter(count => count - n)
}
return decrease
}
export const HawkCounter = () => {
const count = useHawkState(counterState)
const even = useHawkState(isEvenState)
const decrease = useDecrease()
const increase = useIncrease()
return (
<Counter
title="Hawk Counter"
value={count}
helperText={`${count} is ${even}`}
onIncrease={() => increase(1)}
onDecrease={() => decrease(1)}
/>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment