Steven Bell
Steven Bell

Reputation: 963

How do I force a rerender in Svelte when my props changes?

I have a totaling component that needs to total the same index of multiple arrays in a multidimensional array, similar to totaling columns in a spread sheet. I have a different component that allows me to change the values in the arrays, and also totals the rows in the spreadsheet. I'm having troubles with the totaling component re-rendering and/or updating the columns total after a change to the array. I thought I might need to do a reactive declaration in my totaling component, but that doesn't seem to be working.

Here's a link to the REPL.

And here's the code:

Totals.svelte

<script>
    export let jobs

      //this creates a new array which is two indexes long for each of the columns
      //then fills each array with the sum of column
      //HOW DO I GET THIS TO UPDATE EACH TIME A JOB HOURS ENTRY IS CHANGED?
    $: totals = new Array(2).fill(null).map((v, i) => {
    let total = 0;
    jobs.jobHours.forEach((v) => {
      total += Number(v[i]);
    });
    return total;
  });

    //this sums the total hours row to give the grand total
    //HOW DO I GET THIS TO UPDATE EACH TIME THE TOTALS VARIABLE IS CHANGED?
  $: grandTotal = totals.reduce((acc, v) => (acc += v));        

</script>

<style>
  container {
    display: grid;
    grid-template-columns: 2fr 1fr 1fr 1fr;        

  }
  div {
    width: 100%;
    height: 100%;    
    width: 150px;
  }
</style>

<container>
  <div>Total Hours</div>
  {#each totals as total}
  <div>
    {total}
  </div>
  {/each}
  <div>{grandTotal}</div>
</container>
App.svelte

<script>
    import JobEntries from './JobEntries.svelte';
    import Totals from './Totals.svelte';

    const jobs = {
        jobNames: ['Job1', 'Job2', 'Job3'], 
        jobHours: Array.from(Array(3), () => Array.from(Array(2), ()=>1))
    };
</script>

<JobEntries {jobs}/>
<Totals {jobs} />

JobEntries.svelte

<script>
    export let jobs
    let cell = Array.from(Array(3), () => Array.from(Array(3)));
</script>

<style>
  container {
    display: grid;
    grid-template-columns: 2fr 1fr 1fr 1fr;

  }
  input, div {
    width: 100%;
    height: 100%;
    min-width: 150px;
  }
</style>

<container>
    <div>
        Name
    </div>  
    <div>
        Time 1
    </div>
    <div>
        Time 2
    </div>
    <div>
        Total
    </div>
  {#each jobs.jobNames as name, i}
            <input
                type="text"                
                bind:this={cell[i][0]}
                bind:value={name}            />
        {#each jobs.jobHours[i] as hour, j}
            <input
                type="number"                
                bind:this={cell[i][j+1]}
                bind:value={hour}
            />
        {/each}
    <div>
        {jobs.jobHours[i].reduce((acc,v)=>acc+=v)}
    </div>
  {/each}
</container>

Upvotes: 7

Views: 13749

Answers (1)

Steven Bell
Steven Bell

Reputation: 963

Binding the prop in the parent component, allows the prop to be changed by the child component.

App.svelte

<script>
    import JobEntries from './JobEntries.svelte';
    import Totals from './Totals.svelte';
    let jobs = {
        jobNames: ['Job1', 'Job2', 'Job3'], 
        jobHours: Array.from(Array(3), () => Array.from(Array(2), ()=>1))
    };
</script>

<JobEntries bind:jobs/>
<Totals {jobs} />

JobEntries.svelte

<script>
    export let jobs 
    let cell = Array.from(Array(3), () => Array.from(Array(3)));

</script>

<style>
  container {
    display: grid;
    grid-template-columns: 2fr 1fr 1fr 1fr;

  }
  input, div {
    width: 100%;
    height: 100%;
    min-width: 150px;
  }
</style>

<container>
    <div>
        Name
    </div>  
    <div>
        Time 1
    </div>
    <div>
        Time 2
    </div>
    <div>
        Total
    </div>
  {#each jobs.jobNames as name, i}
        <input
            type="text"                
            bind:this={cell[i][0]}
            bind:value={name}            />
        {#each jobs.jobHours[i] as hour, j}
            <input
                type="number"                
                bind:this={cell[i][j+1]}
                bind:value={hour}
            />
        {/each}
    <div>
        {jobs.jobHours[i].reduce((acc,v)=>acc+=v)}
    </div>
  {/each}
</container>

Upvotes: 6

Related Questions