diff --git a/hooks/index.ts b/hooks/index.ts new file mode 100644 index 0000000..6dad8fc --- /dev/null +++ b/hooks/index.ts @@ -0,0 +1,2 @@ +export * from "./useCounter" +export * from "./useThinOut" diff --git a/hooks/useCounter.ts b/hooks/useCounter.ts new file mode 100644 index 0000000..e02f0c6 --- /dev/null +++ b/hooks/useCounter.ts @@ -0,0 +1,30 @@ +import { useDispatch, useSelector } from "react-redux" +import { CounterActions, countSelector } from "../store/counter" + +type CounterOperators = { + count: number + increment: () => void + decrement: () => void + calculate: (inputNumber: number) => void +} + +/** + * Counter custom-hooks + * @see https://reactjs.org/docs/hooks-custom.html + */ +export const useCounter = (): Readonly => { + const dispatch = useDispatch() + + return { + count: useSelector(countSelector), + increment: () => dispatch(CounterActions.increment()), + decrement: () => dispatch(CounterActions.decrement()), + calculate: (inputNumber: number) => { + dispatch( + CounterActions.calculate({ + inputNumber: inputNumber, + }) + ) + }, + } +} diff --git a/hooks/useThinOut.ts b/hooks/useThinOut.ts new file mode 100644 index 0000000..e9618c2 --- /dev/null +++ b/hooks/useThinOut.ts @@ -0,0 +1,47 @@ +import { useDispatch, useSelector } from "react-redux" +import { + IReduxSagaState, + ReduxSagaActions, + reduxSagaDebounceSelector, + reduxSagaThrottleSelector, +} from "../store/redux-saga" + +type ThinOutOperators = { + debounce: (inputValue: string) => void + debounceState: IReduxSagaState + throttle: (inputValue: string) => void + throttleState: IReduxSagaState +} + +/** + * Debounce and throttle custom-hooks + * @see https://reactjs.org/docs/hooks-custom.html + */ +export const useThinOut = (): Readonly => { + const dispatch = useDispatch() + const reduxSagaDebounceState = useSelector(reduxSagaDebounceSelector) + const reduxSagaThrottleState = useSelector(reduxSagaThrottleSelector) + + const handleDebounce = (inputValue: string) => { + dispatch( + ReduxSagaActions.fetchDebounce({ + input: inputValue, + }) + ) + } + + const handleThrottle = (inputValue: string) => { + dispatch( + ReduxSagaActions.fetchThrottle({ + input: inputValue, + }) + ) + } + + return { + debounce: handleDebounce, + debounceState: reduxSagaDebounceState, + throttle: handleThrottle, + throttleState: reduxSagaThrottleState, + } +} diff --git a/pages/redux-saga.tsx b/pages/redux-saga.tsx index 004a0d4..e1690f9 100644 --- a/pages/redux-saga.tsx +++ b/pages/redux-saga.tsx @@ -1,7 +1,6 @@ import { Typography } from "@material-ui/core" import { createStyles, makeStyles, Theme } from "@material-ui/core/styles" import React from "react" -import { useDispatch, useSelector } from "react-redux" import { AppContext } from "../components/AppContext" import { SpacingPaper } from "../components/atoms" import { @@ -10,12 +9,8 @@ import { } from "../components/organisms" import { Layout } from "../components/templates" import { Page, SagaSetting } from "../constants" +import { useThinOut } from "../hooks" import { IPagePayload, PageActions } from "../store/page" -import { - ReduxSagaActions, - reduxSagaDebounceSelector, - reduxSagaThrottleSelector, -} from "../store/redux-saga" const useStyles = makeStyles((_: Theme) => createStyles({ @@ -28,25 +23,7 @@ type Props = {} function ReduxSaga(props: Props) { const {} = props const classes = useStyles(props) - const dispatch = useDispatch() - const reduxSagaDebounceState = useSelector(reduxSagaDebounceSelector) - const reduxSagaThrottleState = useSelector(reduxSagaThrottleSelector) - - const onDebounce = (inputValue: string) => { - dispatch( - ReduxSagaActions.fetchDebounce({ - input: inputValue, - }) - ) - } - - const onThrottle = (inputValue: string) => { - dispatch( - ReduxSagaActions.fetchThrottle({ - input: inputValue, - }) - ) - } + const { debounce, debounceState, throttle, throttleState } = useThinOut() return ( @@ -62,20 +39,20 @@ function ReduxSaga(props: Props) { } - storeState={reduxSagaDebounceState} + storeState={debounceState} responseResultMax={10} interval={SagaSetting.DEBOUNCE_INTERVAL} - onChange={onDebounce} + onChange={(inputValue: string) => debounce(inputValue)} /> throttle(inputValue)} /> diff --git a/pages/redux.tsx b/pages/redux.tsx index bdb6eef..879377e 100644 --- a/pages/redux.tsx +++ b/pages/redux.tsx @@ -7,13 +7,12 @@ import { } from "@material-ui/core" import { createStyles, makeStyles, Theme } from "@material-ui/core/styles" import React, { useState } from "react" -import { useDispatch, useSelector } from "react-redux" import { AppContext } from "../components/AppContext" import { SpacingPaper } from "../components/atoms" import { HeaderArticleContainer } from "../components/organisms" import { Layout } from "../components/templates" import { Page } from "../constants" -import { CounterActions, countSelector } from "../store/counter" +import { useCounter } from "../hooks" import { IPagePayload, PageActions } from "../store/page" const useStyles = makeStyles((theme: Theme) => @@ -38,19 +37,8 @@ type Props = { function Redux(props: Props) { const { defaultInputNumber: defaultCount } = props const classes = useStyles(props) - const dispatch = useDispatch() - const count = useSelector(countSelector) const [inputNumber, setInputNumber] = useState(defaultCount) - - /** - * Increment - */ - const handleIncrement = () => dispatch(CounterActions.increment()) - - /** - * Decrement - */ - const handleDecrement = () => dispatch(CounterActions.decrement()) + const { count, increment, decrement, calculate } = useCounter() /** * Change inputNumber value @@ -63,17 +51,6 @@ function Redux(props: Props) { } } - /** - * Calculate input number - */ - const handleCalculate = () => { - dispatch( - CounterActions.calculate({ - inputNumber: Number(inputNumber), - }) - ) - } - const CurrentNumber = () => ( {count} ) @@ -86,11 +63,11 @@ function Redux(props: Props) { Increment / Decrement -   - @@ -114,7 +91,7 @@ function Redux(props: Props) {