mirror of
https://github.com/Mastermindzh/react-starter-kit.git
synced 2025-08-23 09:35:01 +02:00
Moved examples into example directory
Moved routes to separate file Used route constants
This commit is contained in:
71
src/features/examples/counter/Counter.tsx
Normal file
71
src/features/examples/counter/Counter.tsx
Normal file
@@ -0,0 +1,71 @@
|
||||
import { useState } from "react";
|
||||
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useAppDispatch, useAppSelector } from "../../../app/hooks";
|
||||
import styles from "./Counter.module.css";
|
||||
import { incrementAsync } from "./state/actions/incrementAsync";
|
||||
import {
|
||||
decrement,
|
||||
increment,
|
||||
incrementByAmount,
|
||||
incrementIfOdd,
|
||||
selectCountAndStatus,
|
||||
} from "./state/counterSlice";
|
||||
|
||||
export function CounterContainer() {
|
||||
const { value, status } = useAppSelector(selectCountAndStatus);
|
||||
const dispatch = useAppDispatch();
|
||||
const [incrementAmount, setIncrementAmount] = useState("2");
|
||||
const [translate] = useTranslation();
|
||||
|
||||
const incrementValue = Number(incrementAmount) || 0;
|
||||
|
||||
return (
|
||||
<div>
|
||||
{translate("counter.status", { status })}
|
||||
<div className={styles.row}>
|
||||
<button
|
||||
className={styles.button}
|
||||
aria-label="Decrement value"
|
||||
onClick={() => dispatch(decrement())}
|
||||
>
|
||||
-
|
||||
</button>
|
||||
<span className={styles.value}>{value}</span>
|
||||
<button
|
||||
className={styles.button}
|
||||
aria-label="Increment value"
|
||||
onClick={() => dispatch(increment())}
|
||||
>
|
||||
+
|
||||
</button>
|
||||
</div>
|
||||
<div className={styles.row}>
|
||||
<input
|
||||
className={styles.textbox}
|
||||
aria-label="Set increment amount"
|
||||
value={incrementAmount}
|
||||
onChange={(e) => setIncrementAmount(e.target.value)}
|
||||
/>
|
||||
<button
|
||||
className={styles.button}
|
||||
onClick={() => dispatch(incrementByAmount(incrementValue))}
|
||||
>
|
||||
{/* Setting count allows you to pluralize / display different text based on the count
|
||||
See: https://www.i18next.com/translation-function/plurals
|
||||
*/}
|
||||
{translate("counter.add", { count: incrementValue })}
|
||||
</button>
|
||||
<button
|
||||
className={styles.asyncButton}
|
||||
onClick={() => dispatch(incrementAsync(incrementValue))}
|
||||
>
|
||||
Add Async
|
||||
</button>
|
||||
<button className={styles.button} onClick={() => dispatch(incrementIfOdd(incrementValue))}>
|
||||
Add If Odd
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
Reference in New Issue
Block a user