Passing additional Props to component
See original GitHub issueThank you for making the only Svelte router that has just worked. A huge bonus - it matches the documentation! 😄
I was hoping for a change to the Router component to be able to pass additional props to the rendered component.
Given a router definition the inclusion of the {props} attribute would allow arbitrary data to be passed to the rendered component.
const props = { id: "1234" };
<Router {routes} {prefix} {props} />
Changes required:
1. Include new exported member: export let props; (similar to routes, prefix)
2. Include the {...props} into the component declaration
{#if componentParams}
<svelte:component this="{component}" params="{componentParams}" on:routeEvent {...props} />
{:else}
<svelte:component this="{component}" on:routeEvent {...props} />
{/if}
Rendered component can now access these props in the standard way using export let id;
As per: https://svelte.dev/repl/74593f36569a4c268d8a6ab277db34b5?version=3.12.1
Appreciate your consideration on this additional feature.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Passing Props to a Component - React Docs
React components use props to communicate with each other. Every parent component can pass some information to its child components by giving them...
Read more >How to add additional props to a React element passed in as ...
I am passing a react element as a prop to another element. In the child element that receives the prop, I need to...
Read more >How to use Props in React - Robin Wieruch
As said, there is no way passing props from a child to a parent component. But you can always pass functions from parent...
Read more >How passing props to component works in React
In React, states are passed from one component into another component as props. Since prop names and values will just be passed into...
Read more >How to Pass All Props to a Child Component in React - Medium
The best part about using the spread operator to pass props around in React is that you can add any other props to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi @ItalyPaleAle sorry I’ve been unresponsive to this I would have liked to contribute with the pull request, thank you for doing so on my behalf.
With this change, it looks like the props are exposed as part of the routes configuration and I’ve successfully been able to add them via routes. One difference in the original suggestion was having the props variable exported so that it can be supplied directly on the router component from the parent component rather than via the routes config. Just curious if you can spare a moment 😃 thanks again!
Hey @matt-psaltis thanks again for the contribution and for the convincing argument above!
I initially merged the change just as you proposed above. However, after some testing, I decided to alter the design for two reasons.
loading
component for each route, to be set in thewrap
method. So, I assumed that similar conclusions could be made for this code change too.foo
to the componentBar
. IfBar
doesn’t have that prop (ie.,Bar
doesn’t sayexport let foo
), then you get a runtime warning while in development mode. This is just a warning, and doesn’t appear when compiling in production mode, but it has gotten users annoyed. There’s an issue open with Svelte upstream, but they don’t seem too interested in fixing it. So, I didn’t want to create even more situations when users could see runtime warnings.I know that the new API design is a bit more complex than just passing a prop to the router component… But I hope that the gains in flexibility (and the less annoying warnings) will compensate for the minor additional annoyance of having to use
wrap
.