Zum Hauptinhalt springen
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
YouTube Security VideosNutanix advances legacy and AI app management with AMD(01.10.2026 um 16:00 Uhr)
•
YouTube Security VideosPC-WELT: 32 TB SSD-Speicher in der HMX 6!(01.10.2026 um 16:15 Uhr)
••••
Videos & KonferenzenPC-WELT: 32 TB SSD-Speicher in der HMX 6!(01.10.2026 um 16:15 Uhr)
••
Sicherheitslücken (CVE)USN-8857-1: KCoreAddons vulnerability(01.10.2026 um 12:48 Uhr)
•••
YouTube Security VideosNutanix advances legacy and AI app management with AMD(01.10.2026 um 16:00 Uhr)
•
YouTube Security VideosPC-WELT: 32 TB SSD-Speicher in der HMX 6!(01.10.2026 um 16:15 Uhr)
••••
Videos & KonferenzenPC-WELT: 32 TB SSD-Speicher in der HMX 6!(01.10.2026 um 16:15 Uhr)
••
Sicherheitslücken (CVE)USN-8857-1: KCoreAddons vulnerability(01.10.2026 um 12:48 Uhr)
•••
Intelligence View
⚡ tsecurity.de Intelligence

Share the state of a hook between components

I'm going to show you how to share a hook's state between components. Example We have three components: Label, Increment, and Decrement. The goal is for the…

Beitrag
0
Seite
0
↗ Quelle (dev.to)
Social ReaktionenReagiere als Erste:r — dein Feedback zählt!

I'm going to show you how to share a hook's state between components.






Example



We have three components: Label, Increment, and Decrement. The goal is for the count in the Label to update when either the Increment or Decrement button is clicked.

A live example of following code can be found here:

https://codesandbox.io/p/sandbox/houp-sample-forked-w9rlht




// App.tsx
root.render(
<>
<App />
</>
);

// index.tsx
const App = () => {
return (
<>
<Label />
<Increment />
<Decrement />
</>
);
};

// useCounter.ts
export default function useCounter() {
const [count, setCount] = useState(0);
const decrease = () => setCount(count - 1);
const increase = () => setCount(count + 1);
return {
count,
decrease,
increase,
};
}

// label.tsx
export default function Label() {
const counter = useCounter();
return (
<div>Count: {counter.count}</div>
);
}

// increment.tsx
export default function Increment() {
const counter = useCounter();
return (
<button onClick={counter.increase}>Increment</button>
);
}

// decrement.tsx
export default function Decrement() {
const counter = useCounter();
return (
<button onClick={counter.decrease}>Decrement</button>
);
}






If you run the example from the link above, you'll see that this code doesn't work. This is because a custom hook is designed to share logic, not state directly.






Share a hook's state globally



Let's use https://github.com/houpjs/houp to share useCounter hook's state globally.

A live example of following code can be found here:

https://codesandbox.io/p/sandbox/houp-sample-forked-rfq6j3




// provider.ts
export const Provider = createProvider([useCounter]);

// App.tsx
root.render(
<Provider>
<App />
</Provider>
);

// index.tsx
const App = () => {
return (
<>
<Label />
<Increment />
<Decrement />
</>
);
};

// useCounter.ts
export default function useCounter() {
const [count, setCount] = useState(0);
const decrease = () => setCount(count - 1);
const increase = () => setCount(count + 1);
return {
count,
decrease,
increase,
};
}

// label.tsx
export default function Label() {
const counter = useStore(useCounter);
return (
<div>Count: {counter.count}</div>
);
}

// increment.tsx
export default function Increment() {
const counter = useStore(useCounter);
return (
<button onClick={counter.increase}>Increment</button>
);
}

// decrement.tsx
export default function Decrement() {
const counter = useStore(useCounter);
return (
<button onClick={counter.decrease}>Decrement</button>
);
}






In this revised version, we create a Provider and add it at the root of the app. Then, we call useStore(useCounter) to access the global state.

Now, run the example from the link above, and you'll see that this code works like a charm.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Share the state of a hook between components

Thematisch verwandte Begriffe: Share, state, hook, between · 6 Treffer

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
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