Context provider

Context 공급자

Let deep descendants read a shared value without passing props through every layer.

···
html
<div class="scene"><div class="provider" id="provider">ThemeContext <b>mint</b></div><div class="branches"><div class="branch"><div class="middle">Layout</div><div class="consumer" id="first">Toolbar: mint</div></div><div class="branch"><div class="middle">Page → Panel</div><div class="consumer" id="second">Button: mint</div></div></div><div class="label">intermediate nodes pass no theme prop</div></div>
css
.scene{width:min(94vw,800px);height:min(86vh,320px);position:relative;overflow:hidden;display:flex;flex-direction:column;justify-content:space-between;border:1px solid var(--line);border-radius:14px;background:var(--surface);padding:clamp(9px,2.4vmin,20px);font:500 clamp(15px,4vmin,18px)/1.3 var(--font-sans,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif)}.scene b,.scene strong{font-weight:700}.label{color:var(--muted)}.code{font-family:ui-monospace,SFMono-Regular,Consolas,monospace}.pill{border:1px solid var(--line);border-radius:8px;padding:.4em .65em;background:var(--bg)}.active{border-color:var(--accent)!important;color:var(--accent)!important;background:color-mix(in srgb,var(--accent) 10%,var(--surface))!important}.provider,.middle,.consumer{border:1px solid var(--line);border-radius:7px;text-align:center;padding:clamp(4px,1.6vmin,7px);font-size:clamp(12px,3vmin,15px)}.provider{border-color:var(--accent);font-weight:700}.provider b{color:var(--accent)}.branches{display:flex;gap:8px;flex:1;margin-top:8px}.branch{flex:1;display:flex;flex-direction:column;justify-content:space-around;align-items:stretch;border-top:2px solid var(--accent);position:relative}.branch:before{content:"";position:absolute;top:-9px;left:50%;height:8px;border-left:2px solid var(--accent)}.middle{color:var(--muted)}.consumer{background:color-mix(in srgb,var(--accent) 13%,var(--surface));border-color:var(--accent);font-weight:700}.consumer.pulse{animation:pulse .65s ease}@keyframes pulse{50%{transform:scale(1.04);background:color-mix(in srgb,var(--accent) 28%,var(--surface))}}.label{text-align:center;font-size:clamp(12px,3vmin,14px)}
js
const themes=["mint","amber","blue"];let n=0;function tick(){const theme=themes[n%3];document.querySelector(".provider b").textContent=theme;for(const [id,label] of [["first","Toolbar"],["second","Button"]]){const el=document.getElementById(id);el.textContent=label+": "+theme;el.classList.remove("pulse");void el.offsetWidth;el.classList.add("pulse")}n++}tick();setInterval(tick,1100)

A Context provider supplies a value within a component subtree. Descendants call useContext to read the nearest matching provider, which suits values such as theme or language shared by many components.

The demo updates consumers at different depths when the provider changes. Consumers re-render when their Context value changes, so keep frequently changing, unrelated state in appropriately scoped providers.

When to use

Use it when several components deep in a tree need the same theme, language, or settings value.

Open as page ↗