Zum Hauptinhalt springen
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
•
IT Security NachrichtenIn cloud-native systems, you can’t optimize what you can’t observe(30.09.2026 um 13:00 Uhr)
•
IT Security NachrichtenI Want Better Reporting on AI Genie Behavior(30.09.2026 um 13:05 Uhr)
•
IT Security NachrichtenUK tech execs call for £20 billion investment in sovereign AI(30.09.2026 um 12:57 Uhr)
•
IT Security NachrichtenDeepfake: Betrüger erbeuten 95 Mio. von Privatbank Fideuram(30.09.2026 um 12:57 Uhr)
••
IT Security NachrichtenWearable Nanotech Could Combat Nerve Agents(30.09.2026 um 13:00 Uhr)
•
IT Security NachrichtenX-Plus Storm 10X im Test: Netbook-Format mit bis zu 2 Terabyte SSD(30.09.2026 um 12:45 Uhr)
••••
IT Security NachrichtenIn cloud-native systems, you can’t optimize what you can’t observe(30.09.2026 um 13:00 Uhr)
•
IT Security NachrichtenI Want Better Reporting on AI Genie Behavior(30.09.2026 um 13:05 Uhr)
•
IT Security NachrichtenUK tech execs call for £20 billion investment in sovereign AI(30.09.2026 um 12:57 Uhr)
•
IT Security NachrichtenDeepfake: Betrüger erbeuten 95 Mio. von Privatbank Fideuram(30.09.2026 um 12:57 Uhr)
••
IT Security NachrichtenWearable Nanotech Could Combat Nerve Agents(30.09.2026 um 13:00 Uhr)
•
IT Security NachrichtenX-Plus Storm 10X im Test: Netbook-Format mit bis zu 2 Terabyte SSD(30.09.2026 um 12:45 Uhr)
•••
Intelligence View
⚡ tsecurity.de Intelligence

USING JAVASCRIPT CLOSURES IN REACT

A Closure in Javascript is when a function enclosed inside another function is returned by enclosing function. A closure is formed when the inner function maintains access to variable outside its scope aka lexical scope; access to…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

A Closure in Javascript is when a function enclosed inside another function is returned by enclosing function. A closure is formed when the inner function maintains access to variable outside its scope aka lexical scope; access to variables and arguments of the outer/parent function even after it has executed.



Let's create a tax calculator closure function to calculate taxes of alcoholic and non-alcoholic drinks based on their tax rates.




const taxCalculator = (vat ) => {
return function taxableAmount (amount) {
const tax = amount * vat / 100;
return tax
}
}

//alcoholic drinks have their on VAT, lets say 16%
const alcoholTax = taxCalculator(16)
const alcoholA = alcoholTax(1200) // an Alcohol that costs 12,00
const alcoholB=alcoholTax(800) // an Alcohol that costs 8,00

//non-alcoholic have their own VAT, let say 12%

const nonAlcoholTax = taxCalculator(12);
const water = nonAlcoholTax(500)
const Juice=nonAlcoholTax(300)






As you can see, each drink will always remember their tax rate based on the whether it is alcoholic drink or non-alcoholic.



In react js, JavaScript UI library, event handlers are declared inline on the JSX as such.





<button onClick={handleClick}>Click me</button>






If the event handler has an argument, it will then be invoked inside a function as such.




function ActionButtons(){
const actions = ["Create", "Edit", "Delete"]
const handleAction = (action) => {
switch (action) {
case action[0]:
//do something
break;
case action[1]:
//do something
break;
case action[2]:
//do something
break;

default:
// do nothing
break;
}
}
return (
<div className="flex flex-col md:flex-row w-full p-4 gap-4 ">
{ actions.map(action =>
<button className="w-full md:w-60" onClick={()=>handleAction(action)}>{action}</button>)}
</div>)
}






Note the handleAction is encapsulated by an arrow function when assigning it to the onclick event handler.



With closure we can simply call handleAction with a action argument then return an inner function that grabs the action argument and performs the rest of the actions like so.





function ActionButtons() {
const actions = ["Create", "Edit", "Delete"];
const handleAction = (action) => {
return () => {
console.log(` ${action} Action button clicked`);
switch (action) {
case action[0]:
//do something
break;
case action[1]:
//do something
break;
case action[2]:
//do something
break;

default:
// do nothing
break;
}
};
};
return (
<div className="flex flex-col md:flex-row w-full p-4 gap-4 justify-between">
{actions.map((action) => (
<button className="w-full md:w-60" onClick={handleAction(action)}>
{action}
</button>
))}
</div>
);
}







Notice how we invoke the handleAction directly on the OnClick event? also notice we have refactored the handleAction function so that it returns an arrow function performing the necessary actions?



The handleAction is invoked when the component mounts, a closure occurs when the arrow function returned inside handleAction grabs and holds onto to the value of the argument on handleAction even though it (handleAction) executed during the first render.

It is a neat way of handling events in Javascript.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten USING JAVASCRIPT CLOSURES IN REACT

Thematisch verwandte Begriffe: USING, JAVASCRIPT, CLOSURES, REACT · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

💬 Kommentare werden geladen…
Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-102911 | A flaw has been found in zosmaai pi-llm-wiki up to 0.11.7. Affected is …
Advisory →
tsecurity.de Icon
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag