Transfer items among components

I've been trying to achieve a specific functionality with my objects. Even if the code isn't functioning correctly, I hope you can grasp the intention.

In Nested.svelte, I have this component with the specified coordinates:

<script lang=ts>
    export let pos: {
        x: number,
        y: number,
    }
</script>

Each nested component in my program is displayed like this in App.svelte:

{#each objects as object}
    <Nested bind:pos.x={object.geometry.x} bind:pos.y={object.geometry.y}
{/each}

The objects variable holds a store value structured as follows:

objects.geometry = [
    {
        x,
        y,
    }, {
        x,
        y,
    } ...
]

The issue lies in passing objects.geometry.x and objects.geometry.y to the Nested component from the App. The provided code does not function properly, and the only workaround I have found is passing the entire object:

<Nested bind:pos={objects.geometry}>
. However, this method does not allow me to bind specific values in App to specific values in Nested. It also requires that the variables in the objects variable have the same names as the coordinate values in Nested.svelte (x and y).

Is there an alternative way to pass object-structured data into components? Perhaps something that would operate similar to

<Nested bind:pos.x={object.geometry.x} bind:pos.y={object.geometry.y}
?

Answer №1

If you want to customize your own pos variable using the exported values of x and y, you can achieve it like this:

export let x;
export let y;

$: pos = new Proxy({x, y}, {
    set: (_obj, key, value) => {
        if (key === "x") x = value;
        else if (key === "y") y = value;
        else return false;
        return true
    }
})

repl
(If you don't need to modify pos, you can also use $: pos = { x, y };. This will keep it updated without bidirectional binding as implied by your usage of bind)

However, I recommend simply working with x and y directly.

Answer №2

Unfortunately, this feature is not currently supported.

If you wish to avoid using a binding (which is recommended), you can instead inline an object like this:

{#each items as item}
    <CustomComponent position={{ x: item.coordinates.x, y: item.coordinates.y }} />
{/each}

Alternatively, you will need to separate the position property into individual top-level properties x and y.

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

Tips for avoiding the bootstrap modal dialog background from taking over the entire screen

Is there a way to utilize a Bootstrap Modal dialog without altering the positioning of a div? I have encountered an issue where when adding a <div class='modal hide fade'> dialog into a fixed positioned div (position:fixed in css) and calli ...

creating dynamic lists in angular.js

Could you please guide me on creating a dynamic list in Angular.js? I have successfully implemented a feature where users can add items to the list by clicking a button and filling out the required fields. To see this in action, check out this fiddle: http ...

Passing an arrow function as props results in an error of undefined

When passing event handler functions as props to child components, regular functions are received but not fat arrow functions. import React, { Component } from "react"; export default class FruitClass extends Component { bananaEvents = { color: thi ...

How can the file system module (fs) be utilized in Angular 7?

Hello, I am currently working with Angular 7. I recently attempted to utilize the fs module in Typescript to open a directory. However, I encountered an error message stating: "Module not found: Error: Can't resolve fs". Despite having types@node and ...

Error in Typo3 Rendering FieldControl due to insufficient arguments

A plugin has been created for a Typo3 retailer/store database that requires latitude and longitude data for each store. To streamline the process, a button has been added to automatically enter this data through field control in TCA. While this works well ...

Capture the PHP stylesheet output and save it as a variable

I am attempting to retrieve the output of a PHP file and save that information into a variable on a different page. The PHP file is set with a content type of text/css in its stylesheet. Once I successfully store the content in a variable, my plan is to us ...

How can the callback from C++ be successfully installed in the Javaobject window within QtWebkit?

I have successfully implemented HTML-JS to call a C++ method using QtWebkit. Now, I want to send a callback from the C++ method to the JavaScript window. How can I achieve this? Below is my code snippet. #include <QtGui/QApplication> #include <Q ...

How come code suggestion works flawlessly on Stackblitz, yet fails to function in Intellij?

Check out this GitHub repository for a simple way to reproduce the issue: https://github.com/tmtron/mathjs-typescript-types Successful Code Completion in Stackblitz Everything works perfectly when you open the project in Stackblitz: https://i.stack.imgur ...

The process of setting up React in the terminal becomes tricky when the VS code editor is directing to a non-existent path

Issue with VS Code editor not recognizing existing path Recently attempted to install React using the npx command in the terminal, following steps from various tutorials on YouTube. Despite trying all suggested methods, the installation didn't succee ...

Load the PHP elements once the nav tab has been switched

I have a project with 10 tabs, each of them fetches data from an API using PHP. The loading time for the page is around 10-15 seconds since it updates all values for the 10 tabs. Is there a way to load content only when switching between tabs? Start by l ...

Guide to creating a custom wrapper for a Material UI component with React JS

I am utilizing the Material UI next library for my project and currently working with the List component. Due to the beta version of the library, some parameter names are subject to change. To prevent any future issues, I have decided to create a wrapper a ...

Guide to developing a dynamic method while utilizing IntelliSense assistance

When utilizing itemsApi in React reduxTool kit, dynamic functions like use..., mutate... are generated. These dynamically created methods possess complete intelligence as well. For instance, if you have createApi({... getItems: builder.query<any, st ...

Is Q.js capable of functioning independently from node.js and require()?

Currently experimenting with the latest version of q.js to integrate promises into my ajax calls without utilizing node.js at all. I retrieved the most recent version from https://github.com/kriskowal/q and only included q.js in my project. While testing, ...

Is there a way to instantiate a new object within the same for loop without replacing the ones created in previous iterations?

My current issue with this exercise is that as I convert the first nested array into an object, the iteration continues to the next nested array and ends up overwriting the object I just created. I'm wondering how I can instruct my code to stop itera ...

The error message being thrown is: "Cannot access the 'top' property of an undefined value with the offset().top-500 method."

As a beginner in testing, I encountered a console error on my page while scrolling. The error message displayed was: main.js:18 Uncaught TypeError: Cannot read property 'top' of undefined at main.js:18 at dispatch (jquery-1.9.js:3) at v.han ...

Unable to locate module in node_modules directory when attempting to import an image into TS

In my .tsx file, I am encountering an issue with the following code snippet: import L from 'leaflet'; import icon from 'leaflet/dist/images/marker-icon.png'; import iconShadow from 'leaflet/dist/images/marker-shadow.png'; The ...

I encountered an error stating that "next is not a function." Interestingly, this code works perfectly fine with another source that was recommended by a friend

const express=require('express'); const app=express() //middleware const customMiddleware=(req,res,next)=>{ console.log('this is a custom middleware that will be executed before the route handler'); next(); } customMiddlewar ...

Expanding MaterialUi styled components by incorporating TableCellProps: A guide

When trying to create a styled TableCell component in a separate file, I encountered an error in TypeScript (ts(2322)). TypeScript was indicating that the properties "component," "scope," and "colSpan" could not be used because they do not exist in StyledC ...

Plupload - Resize image upon upload with minimum height and width requirements, rather than maximum requirements

I am currently using a Plupload uploader that creates both a thumbnail and a large size image for each upload. The issue I am facing is with resizing the thumbnail to fit into a 150x150px box. I have tried using some javascript to scale down the image, but ...

Ways to prompt rerendering in child components?

Currently, I am initializing child components within the parent and storing them in an array. I pass a function into the children's props that retrieves state. <QuestionContainer error={ () => this.state.currentError } /> The issue ari ...