Skip to content

Commit de76116

Browse files
committed
use callback
1 parent 3449d55 commit de76116

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

optimization/src/App.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1-
import React, { useState } from 'react';
1+
import React, { useState , useCallback} from 'react';
22
import Button from "./components/UI/Button/Button"
33

44
import './App.css';
55
import DemoOutput from './components/Demo/DemoOutput';
66

77
function App() {
88
const [showPara , setShowPare] = useState(false)
9+
const [allwToggle , setAllowToggle] = useState(false)
910
/** Function is an object is Javascript */
10-
const ToggleParaHandler = () =>{
11-
setShowPare( (prevShowPara) => !showPara )
11+
const ToggleParaHandler =useCallback( () =>{
12+
if(allwToggle){
13+
setShowPare( (prevShowPara) => !prevShowPara )
14+
}
15+
} ,[allwToggle] )
16+
17+
const allowToggleHandler = () => {
18+
setAllowToggle(true)
19+
1220
}
1321

1422

1523
return (
1624
<div className="app">
1725
<h1>Hi there!</h1>
1826
<DemoOutput show={showPara}/>
27+
<Button onClick={allowToggleHandler}>Allow Toggle</Button>
1928
<Button onClick={ToggleParaHandler}>Toggle Para!</Button>
2029

2130
</div>

0 commit comments

Comments
 (0)