Can svelte:self be used to add to nested JSON?

I am currently working on targeting nested JSON using svelte:self but I have run into an issue. Whenever I try to add new entries to the JSON, they end up getting deleted if they are positioned lower in the JSON structure. You can take a look at this example REPL that showcases the problem with the svelte:self demo. It seems like there is something missing in how I have designated where to insert new entries in the JSON?

<script>
let files = [
  {
    name: 'entry 1',
    files: [
      { 
        name: 'nested entry' 
      }
    ]
  },        
  { 
    name: 'entry 2' 
  }
];

function add() {
  files = files.concat({name: 'new item'})
}
</script>

<ul>
{#each files as file}
<li>
  {#if file.files}
    <svelte:self {...file}/>
  {:else}
    <File {...file}/>
  {/if}
</li>
{/each}
<li>
  <button on:click={() => add()}>new</button>
</li>
</ul>

Answer №1

To avoid unwanted deletions when using <svelte:self {...file}>, it is recommended to set the props separately and include 'bind:' before the files in both the child component and the main <Folder ..> tag. This approach helps prevent unexpected changes -> [REPL] ()

App.svelte
<Folder name="Home" bind:files={root} expanded/>
Folder.svelte
<svelte:self name={file.name} bind:files={file.files}/>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Difficulty navigating through pages on an iPad due to slow scrolling with JavaScript

My operation is executed within a scroll function, like this: Query(window).scroll(function(){ jQuery('.ScrollToTop').show(); // my operation. }); While my web page responds quickly to the operation, it seems slo ...

Capture locations from Google Maps

What is the best method to extract all addresses, along with their latitude and longitude, for a specific city (such as Bangalore) from Google Maps using PHP/JavaScript and store it in a MySQL table? I urgently need assistance. Thank You ...

How can you nest a map within a map in JavaScript?

Here is the code snippet I am working with: _renderChannels() { return this.state.channelsData.map(channelData => { return this.state.channelsStreamData.map(channelStreamData => { return <Channel channelData={channelData} ch ...

Error: jwt_decode function has not been declared

I'm currently working on a project and I've hit a roadblock while trying to fetch profile information for the logged-in account using a token. Despite using the JWT-decoding library, I keep encountering the issue where jwt_decode is not defined. ...

Determining the True Width of a Div

I am working with a div that has a class of myid_templates_editor_canvas-selection. <div class="myid_templates_editor_canvas-selection" style="position: absolute; font-size: 0px; left: 0px; top: 0px; width: 377px; height: 174px;"></div> The w ...

Tips for removing duplicate objects from an array

I am utilizing the underscore.js plugin in my code I experimented with it on jsfiddle var basket=[{ "basketitems":[{"items":[]}], "itemdetails":[{ "amountPledged": "100", "bActivity": "Handloom Wo ...

Calculating the total sum of values from keys, post applying the filterBy method

HTML: <div id="app"> <h1>{{title}}</h1> <form id="search"> Filter <input name="query" v-model="filterQuery"> </form> <table> <tr v-for="client in clients | filterBy filterQuery"> <td ...

Twice within a controller, alert is displayed in AngulasJS, OnsenUI, and Phonegap

I'm currently developing a mobile application using AngularJS, OnsenUI, and PhoneGap to package it for Android devices. However, I've encountered a strange issue where an alert box pops up twice when the application is run as a new process. I&ap ...

A step-by-step guide on how to substitute document.write with document.getElementById('ElementID').innerHTML

As someone who is not a programmer, I often find myself attempting to grasp code and make adjustments through trial and error. Currently, I am faced with the challenge of modifying the document.write function in the function pausescroller within a Joomla m ...

How can I retrieve a formController in AngularJS?

When trying to reset the data in a form and calling form.setPristine(), I encounter an issue where the formController is not registered within the $scope. It may sound like a basic question, but how can I locate the formController? Within the code snippe ...

Is there a way to find all records created at a particular time daily through a query?

I understand how to search for documents within a particular range, but I am unsure of the query needed to retrieve all documents in a collection that were created at 3PM. Assuming there is a field called createdAt where this information is stored as Jav ...

developing a shader that transitions between day and night based on the movement of a light source

I've set up a scene with a sphere illuminated by a DirectionalLight to simulate the sun shining on Earth. My goal is to incorporate a shader that displays the earth at night on the unlit portions of the globe and during the day on the lit areas. Event ...

Obtain information using AJAX calls with jQuery Flot

Having an issue with jQuery Flot that I need help resolving. PHP output (not in JSON format): [[1, 153], [2, 513], [3, 644]] ~~ [[1, 1553], [2, 1903], [3, 2680]] Here is the jQuery call: $.ajax({ url: 'xxx.php', success: function (dat ...

Get the value from AJAX and input it into the text box through PHP

I'm attempting to retrieve database results to check the availability of a username, but I'm not receiving any Ajax response. Below is the HTML code: <form id="user_form"> <input placeholder="Enter username" type="text" name="aj ...

Optimizing the performance of "document.createElement"

When attempting to display multiple rows of data in a popup using a for loop, I initially utilized text strings to create and append the div elements. However, I discovered that using document.createElement resulted in a 20% improvement in performance. D ...

What is preventing me from being able to manipulate the data retrieved from this ajax request?

I've been attempting to make an ajax call to my json server on localhost:3000 in order to retrieve an object that I can manipulate and display on a webpage. However, no matter what I try, I just can't seem to console log the (data) so that I can ...

The cloud function in Firebase was unable to connect to the dialogflow route

I am currently working on creating a chatbot using Dialogflow integrated with OpenAI in Firebase Cloud Function. However, I am facing an issue where I cannot access the /dialogflow endpoint and keep receiving the error message: "Cannot GET /dialogflow". My ...

I am interested in implementing a "slide up" effect for the "slide menu" when it loses focus

When I focus out of the asmenu class, I attempt to trigger a 'slide up' effect. However, if I select two checkboxes from the child elements within the asmenu class, the focusout event is still triggered. Ideally, I would like the 'slide up& ...

"Jackson, can you provide guidance on how to interpret this particular

I am dealing with an object structure that looks like this: {"1" : {OBJECT}, "2" : {OBJECT}, "3" : {OBJECT}, ..., "title" : "RandomTitle"} Simply using a map like Map<String, OBJECT> is not possible in this case because the last item is a String ...

Mapping JSON objects with Knockout.js - specifically targeting the initial array

After receiving the JSON from my service, it will contain the "first" Array and a ResponseStatus array. {"Customers":[{"Id":1,"Name":"Thomas","LastName":"Deutsch"}, {"Id":2,"Name":"Julia","LastName":"Baumeistör"}],"ResponseStatus":{}} The first JSON Arr ...