<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Nguyen Dang Khoa</title>
    <description>The latest articles on Forem by Nguyen Dang Khoa (@wentallout).</description>
    <link>https://forem.com/wentallout</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F628400%2F02dedb06-4b57-4adb-8c4e-28823162e04d.png</url>
      <title>Forem: Nguyen Dang Khoa</title>
      <link>https://forem.com/wentallout</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/wentallout"/>
    <language>en</language>
    <item>
      <title>How to send data from child component to parent component through &lt;slot&gt; in Svelte</title>
      <dc:creator>Nguyen Dang Khoa</dc:creator>
      <pubDate>Tue, 20 Sep 2022 12:45:28 +0000</pubDate>
      <link>https://forem.com/wentallout/how-to-send-data-from-child-component-to-parent-component-through-in-svelte-37a3</link>
      <guid>https://forem.com/wentallout/how-to-send-data-from-child-component-to-parent-component-through-in-svelte-37a3</guid>
      <description>&lt;p&gt;&lt;em&gt;I was using SvelteKit version 1.0.0-next.483 when I wrote this. Things might change due to Svelte being young.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;So I'm used to throwing data from a parent container to a child component in Svelte but what if I wanted to do the opposite?&lt;/p&gt;

&lt;p&gt;Here's the problem I had in my project:&lt;/p&gt;

&lt;p&gt;This is the child component called SectionTitle.svelte&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="section-title"&amp;gt;
    &amp;lt;div class="section-icon"&amp;gt;
        &amp;lt;slot /&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;h2 class="large-text"&amp;gt;{sectionTitle}&amp;lt;/h2&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the parent that uses SectionTitle.svelte&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;SectionTitle sectionTitle="Book" let:sectionIcon&amp;gt;
    &amp;lt;Books color="red" width="32" height="32" /&amp;gt;
&amp;lt;/SectionTitle&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So my SectionTitle component needs a &lt;code&gt;&amp;lt;slot&amp;gt;&lt;/code&gt; so that when I import it I can throw in an icon component called &lt;code&gt;&amp;lt;Books&amp;gt;&lt;/code&gt;. It works fine... until I realized every single icons I put in SectionTitle need those properties: &lt;code&gt;color="red" width="32" height="32"&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  My solution
&lt;/h2&gt;

&lt;p&gt;I need to somehow let my child has its own default props value to release its parents from pain&lt;/p&gt;

&lt;p&gt;SectionTitle.svelte&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
    export let sectionTitle = '';

    let sectionIcon = {
        color: 'var(--primary-600)',
        height: 32,
        width: 32,
    };
&amp;lt;/script&amp;gt;

&amp;lt;div class="section-title"&amp;gt;
    &amp;lt;div class="section-icon"&amp;gt;
        &amp;lt;slot sectionIcon={sectionIcon} /&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;h2 class="large-text"&amp;gt;{sectionTitle}&amp;lt;/h2&amp;gt;
&amp;lt;/div&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then I import and used SectionTitle.svelte like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;SectionTitle sectionTitle="UI + UX" let:sectionIcon&amp;gt;
    &amp;lt;FigmaLogo {...sectionIcon} /&amp;gt;
&amp;lt;/SectionTitle&amp;gt;

&amp;lt;SectionTitle sectionTitle="Book" let:sectionIcon&amp;gt;
    &amp;lt;Books {...sectionIcon} /&amp;gt;
&amp;lt;/SectionTitle&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now I have some default values for my icons, before I even know what icon components to put inside the &lt;code&gt;&amp;lt;slot&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I'm still learning Svelte. If you have any suggestions, please comment down below.&lt;/p&gt;

</description>
      <category>svelte</category>
      <category>tutorial</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
