0
0
Svelteframework~5 mins

setContext in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is setContext used for in Svelte?

setContext lets a parent component share data or functions with its child components without passing props directly.

Click to reveal answer
beginner
How do you retrieve data set by setContext in a child component?

You use getContext with the same key to access the shared data or functions.

Click to reveal answer
intermediate
Why use setContext instead of passing props in Svelte?

It avoids 'prop drilling' by letting deeply nested components access data directly from an ancestor.

Click to reveal answer
beginner
What type of value can be passed with setContext?

Any JavaScript value: objects, functions, primitives, or stores can be shared via setContext.

Click to reveal answer
intermediate
Can setContext be used multiple times with different keys in the same component?

Yes, you can call setContext multiple times with different keys to share multiple pieces of data.

Click to reveal answer
What does setContext do in Svelte?
AHandles user events
BCreates a new component
CShares data from parent to child without props
DUpdates the DOM directly
Which function retrieves data set by setContext?
AuseContext
BreadContext
CfetchContext
DgetContext
What must you provide as the first argument to setContext?
AA unique key
BA component name
CA CSS selector
DAn event handler
Can setContext share reactive stores?
AYes, any JavaScript value including stores
BNo, only static values
COnly strings and numbers
DOnly functions
If a child component does not call getContext, what happens to the context data?
AIt is automatically passed as props
BIt is ignored and not accessible
CIt causes an error
DIt is logged to the console
Explain how setContext and getContext work together in Svelte to share data between components.
Think about how you pass a secret message down a chain without telling everyone.
You got /4 concepts.
    Describe a situation where using setContext is better than passing props in Svelte.
    Imagine you want to give a tool to a friend far away without handing it through many people.
    You got /3 concepts.