Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import React from 'react';
import logo from './logo.svg';
import React, { useState } from 'react';
import {usePrevious} from './hooks/UsePrevios'
import './App.css';
import styles from './hooks/UsePrevios.module.css'

function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.tsx</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
const Counter = () => {
const [value, setValue] = useState<number>(0);
const previousValue = usePrevious(value);

return (
<div className={styles.box}>
<div className={styles.flex}>
<h2 className={styles.current_value}>Current value: {value}</h2>
<h2 className={styles.previous_value}>Previous value: {previousValue}</h2>
</div>
<button
className={styles.increment}
onClick={() => {
setValue((prevState) => prevState + 1);
}}
>
Increment
</button>
</div>
);
}

export default App;
export default Counter;
43 changes: 43 additions & 0 deletions src/hooks/UsePrevios.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.increment{
width: 140px;
height: 60px;
border-radius: 10px;
border: none;
background-color: darksalmon;
font-size: 20px;
font-weight: 600;
color: #25140f;
transition: 0.5s;

}
.increment:hover{
background-color: #985e4b;
color: #b6b1b1;
}
.current_value{

}
.previous_value{

}
.box{
width: 45vw;
height: 45vh;
display: flex;
justify-content: space-evenly;
align-items: center;
flex-direction: column;
background-color: bisque;
margin-top: 180px;
margin-left: 390px;
border-radius: 17px;
}
.flex{
width: 48vw;
display: flex;
justify-content: space-evenly;
align-items: center;
margin-top: 15px;
font-size: 21px;
color: #3a2119;
}
12 changes: 12 additions & 0 deletions src/hooks/UsePrevios.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useEffect, useRef } from 'react';

export const usePrevious = (value: number) => {
const ref = useRef<number>();

useEffect(() => {
ref.current = value;
}, [value]);

return ref.current;
};