What is the right way for passing data between 2 child components in svelte

Alex

What is the right way for me to pass data between 2 child components? What I have is a pretty straightforward structure:

APP.svelte

<script>
        import Carmaterial from "./Carmaterial.svelte";
        import Carcolor from "./Carcolor.svelte";

        const carMaterial = {
            title: 'Please select car material',
            property: 'carMaterial',
            options: [
                {id: 0, name: 'Gold'},
                {id: 1, name: 'Titanium'},
                {id: 2, name: 'Silver'}
            ],
        };
    
        const carColor = {
            title: 'Please select car color',
            property: 'carColor',
            options: [
                {id: 0, name: 'Black'},
                {id: 1, name: 'Blue'},
                {id: 2, name: 'Orange'},
                {id: 3, name: 'White'},
                {id: 4, name: 'Yellow'},
                {id: 5, name: 'Green'},
            ],
        };
            
    </script>
    
    //here im passing all of the data to the components itself

    <Carmaterial {...carMaterial} />
    <Carcolor {...carColor}/>
    
    
    <style>
    
    </style>

The components are basically 2 sets of radio inputs:

Carmaterial.svelte

<script>

export let property;
export let title;
export let options;

//im using this in order to predefine value
export let selected = options[1];

</script>
<h3>{title}</h3>
<ul>
{#each options as option (option.id)}

    <li>
        <label>
            <input 
                value={option.id} 
                bind:group={selected.id} 
                type="radio" 
                name={property}>
            {option.name}
        </label>
        
    </li>

{/each}
</ul>

<style lang="scss">

</style>

Carcolor.svelte

<script>
export let property;
export let title;
export let options;

//im using this in order to predefine value
export let selected = options[2];

</script>
<h3>{title}</h3>
<ul>
{#each options as option (option.id)}

    <li>
        <label>
            <input 
                value={option.id} 
                bind:group={selected.id} 
                type="radio" 
                name={property}>
            {option.name}
        </label>
        
    </li>

{/each}
</ul>
<style lang="scss"></style>

in the end my output looks like

h3 with Title
ul li with material options
h3 with Title
ul li with color options

What im trying to achieve is -> I need somehow to disable "Orange" and "White" radio buttons (from carColor group) when "Titanium" material (from carMaterial group) is selected.

Is this possible without using Shop? And if yes, what would be the best solution in case I don't want to messed up my data flow..

Stephane Vanraes

There are basically two ways:

  1. use a store, this would provide a 'shared' place for the components to get their data

  2. move the data to the parent component and pass it on to the children:

<script>
 let selectedMaterial = 'paper';
 let selectedColor = 'orange';
</script>

<Carmaterial {...carMaterial} {selectedColor} bind:selectedMaterial />
<Carcolor {...carColor} {selectedMaterial} bind:selectedColor />

Note that instead of using selected I would be using selectedMaterial and selectedColor in the respective component just to keep it shorter.

The basic principle is easy to understand though: bind the selection from 1 component to the parent, pass this selection to the other component.

You are already keeping the possible data in the parent, this seems like a natural thing to do.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Passing data into "router-outlet" child components

Angular2: Passing data to child components

angular2 - passing data object between components

What is the right way to send data between modules in AngularJS?

Passing data to child components in reactjs

Passing data between child components

What is the difference between passing data in this way to properties in React?

Passing data from service to Component --> Child Components

Passing data between unrelated components

What is the best way to communicate and pass data between child polymer elements?

React Context not passing data to its child components

What is the best way to show loading bar until data in all child and sub child components arrive in Angular

What is the best way to signal 50+ nested components to execute a child component function in svelte 3

Passing Data between react components

Best way to share data between two child components in Blazor

Passing data child to parent functional components in reactjs

Passing data between sibling components in Svelte

ReactJS: Issues when passing data from parent to child component right after fetching data from API. (functional components only)

Passing values between child components in ngFor

Passing data between components in Angular 2 with <router-outlet>

Angular2 passing data between NON parent - child components

Passing data between components issue

what's the best way to share user data between components?

Passing Data between sibling components

What are some alternatives in vue.js to passing data to child components other than props

Passing a list of components in Svelte

What is the best possible way to pass data between child components in React?

Passing Data between vue components

Passing data between components VueJS2