Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Unix & Linux ServerSecurity: Mangelnde Eingabeprüfung in perl-Authen-SASL (SUSE)(23.09.2026 um 00:02 Uhr)
Unix & Linux ServerSecurity: Mangelnde Rechteprüfung in Sudo (Ubuntu)(23.09.2026 um 00:02 Uhr)
Sichere ProgrammierungAutomating Bug Reports with Tally and Make(22.09.2026 um 22:44 Uhr)
Sichere ProgrammierungHTB - Forest(22.09.2026 um 22:49 Uhr)
Sichere ProgrammierungYour Ports Are Lying About Your Business(22.09.2026 um 22:52 Uhr)
Sichere ProgrammierungOLTP vs. OLAP: Understanding the Foundation of Modern Data Systems(22.09.2026 um 22:54 Uhr)
Unix & Linux ServerSecurity: Mangelnde Eingabeprüfung in perl-Authen-SASL (SUSE)(23.09.2026 um 00:02 Uhr)
Unix & Linux ServerSecurity: Mangelnde Rechteprüfung in Sudo (Ubuntu)(23.09.2026 um 00:02 Uhr)
Sichere ProgrammierungAutomating Bug Reports with Tally and Make(22.09.2026 um 22:44 Uhr)
Sichere ProgrammierungHTB - Forest(22.09.2026 um 22:49 Uhr)
Sichere ProgrammierungYour Ports Are Lying About Your Business(22.09.2026 um 22:52 Uhr)
Sichere ProgrammierungOLTP vs. OLAP: Understanding the Foundation of Modern Data Systems(22.09.2026 um 22:54 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Coding Challenge Practice - Question 16

The task is to create a useFocus() hook that allows conditional rendering in parent element to focus on the state of descendant elements. The boilerplate code is: import React, { Ref } from 'react' export function useFocus<T…

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

The task is to create a useFocus() hook that allows conditional rendering in parent element to focus on the state of descendant elements.



The boilerplate code is:







import React, { Ref } from 'react'

export function useFocus<T extends HTMLElement>(): [Ref<T>, boolean] {
// your code here
}

// if you want to try your code on the right panel
// remember to export App() component like below

// export function App() {
// const [ref, isFocused] = useFocus()
// return <div>
// <input ref={ref}/>
// {isFocused && <p>focused</p>}
// </div>
// }







The first step is to create a ref for the input, and also a state variable for focusing on the input




const ref = useRef<T>
const[isFocused, setIsFocused] = useState(false)






The state is a boolean because the input is either focused or not. The next step is to get the node. If there is no node, return null.




const node = ref.current
if(!node) return






Next, a function that sets isFocused to true when the focus moves into the container or any descendant




const handleFocusIn = () => setIsFocused(true)






Then, a function that sets isFocused to false when the focus is outside the container or any descendant. It receives the FocusEvent and uses event.relatedTarget to see where focus is moved to.




const handleFocusOut = (e: FocusEvent) => {
if(node && !node.contains(e.relatedTarget as Node)) {
setIsFocused(false)
}
}






Event listeners for focusin and focusout should be added to the node.




node.addEventListener("focusin", handleFocusIn)
node.addEventListener("focusout", handleFocusOut)






A function to clean up previous event listeners when the app renders again




return () => {
node.removeEventListeners("focusin", handleFocusIn)
node.removeEventListeners("focusout", handleFocusOut)
}






The final code with an example looks like this:







import React, { useRef, useEffect, useState, Ref } from 'react'

export function useFocus<T extends HTMLElement>(): [Ref<T>, boolean] {
// your code here
const ref = useRef<T>(null);
const[isFocused, setIsFocused] = useState(false);

useEffect(() => {
const node = ref.current
if(!node) return

const handleFocusIn = () => setIsFocused(true);
const handleFocusOut = (e: FocusEvent) => {
if(node && !node.contains(e.relatedTarget as Node)) {
setIsFocused(false)
}
}

node.addEventListener("focusin", handleFocusIn)
node.addEventListener("focusout", handleFocusOut)

return () => {
node.removeEventListener("focusin", handleFocusIn)
node.removeEventListener("focusout", handleFocusOut)
}
})
return [ref, isFocused]
}

// if you want to try your code on the right panel
// remember to export App() component like below

export function App() {
const [ref, isFocused] = useFocus<HTMLDivElement>()
return <div ref={ref}>
<input placeholder="First Input"/>
<input placeholder="Second Input" />
{isFocused && <p>focused</p>}
</div>
}







That's all folks!

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Coding Challenge Practice - Question 16

Thematisch verwandte Begriffe: Coding, Challenge, Practice, Question · 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 ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-62985 | request-filtering-agent is an http(s).Agent implementation that blocks r…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
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
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick