Note: Self-answer provided.
There are three primary methods in Svelte for passing data between components:
1. Utilizing Props
This involves passing data from a parent component to a child component.
- Data transfer is one-way only.
- Data can only be passed between immediate parent-child components.
2. Implementing and utilizing Context
Data can be passed from a parent to any nested child component.
- Data flow is unidirectional.
- Data cannot be passed outside the parent-child hierarchy.
- Context must be established when the parent component is initialized.
3. Using Svelte Store
This allows for setting, updating, and subscribing to data from any file or component.
- If multiple instances of the same app run, the store will retain only one value from either instance.
storeVar.set()
sets the value based on page load time.storeVar.update()
uses the value from the last instance.
Purpose
The goal is to configure a Svelte app that detects multiple custom embed codes within the root index file and runs distinct instances of the app. Each embedded code includes additional data such as video size and aspect ratio. Consequently, the UI for each instance will adjust according to the attributes of the embedded code (passed as props to the App). Users may interact with the UI by clicking on different actions like screen sizes, which should trigger updates specific to that particular instance without affecting others.
Challenge
The current issues involve limitations in passing or updating values within components after initialization (issue 2.2, 2.3), or encountering difficulties where the store fails to retain values for every instance (3.1).