Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make a child component inside a slot to send data to his parent?

Tags:

svelte

I'm trying to create a Form component in Sveltejs. This Form component will use <slot /> to let his parent (App.svelte) define which Inputs and Buttons should be inside the Form. The Inputs and Buttons are components too and once the Input component's value changes I'd like to update an object inside the parent Form component and when Button is clicked I'd like to send data back to the App component.

I've tried to read Svelte documentation and solve this problem playing around with <slot let:name={value}> but I can't find a way to update Form component when an Input component's value changes.

This is the structure of what I'm trying to do:

App.svelte

...
<Form on:submit={saveReceivedData}>
  <Input name="..." value="..." />
  <Button />
</Form>
...

Form.svelte

<script>
...
let data = {}
</script>
...
<form>
  <slot />
</form>
...

Input.svelte

...
<input name={name} value={value} on:input={updateDataVariableOnFormComponent} />
...

Button.svelte

...
<button on:click={sendDataVariableToAppComponent}>Send</button>
...

I can try to send you a full code if needed. But, since my problem is not "what I'n doing wrong?" but "how can I do this?", I prefer to write my question in an abstract way.

like image 689
Vincenzo Lombino Avatar asked Jul 02 '19 17:07

Vincenzo Lombino


People also ask

Can we send data from a child component to a parent component?

To pass data from child to parent component in React:Pass a function as a prop to the Child component. Call the function in the Child component and pass the data as arguments. Access the data in the function in the Parent .

How do you emit data from child to parent?

Send data from the Child Component to Parent Component using the $emit() method. STEP 01: Invoke the $emit() method in the child component where you want to send a piece of the data to its parent component.

Can we pass data from child to parent in angular?

The Angular documentation says "The @Output() decorator in a child component or directive lets data flow from the child to the parent." This is exactly what we want.

How do you send data to child components?

Sending data to a child componentlinkThe @Input() decorator in a child component or directive signifies that the property can receive its value from its parent component. To use @Input() , you must configure the parent and child.


1 Answers

You can use a component binding to update data in the parent when the child component changes:

<Input name="..." bind:value={someValue} />

With that binding, someValue in the parent will be synced to value in the child.

To notify the parent about events in the child, as opposed to state changes, you can either use an event dispatcher...

<!-- Button.svelte -->
<script>
  import { createEventDispatcher } from 'svelte';
  const dispatch = createEventDispatcher();
</script>

<button on:click={() => dispatch('thinghappened', someData)}>Send</button>

...in which case you could listen for the thinghappened event...

<Button on:thinghappened={e => doSomethingWith(e)}/>

...or you can simply forward the DOM event with <button on:click>Send</button> and listen for the click event in the parent with <Button on:click={handler}/>.

like image 91
Rich Harris Avatar answered Oct 07 '22 21:10

Rich Harris