question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Request: support top-level component props

See original GitHub issue

Right now, it seems to be required that you have a single component property called params on your components to be able to route to them.

For example:

const routes = {
    '/:foo/:bar': MyComponent
}

requires a component that declares:

export let params = {};

And accesses the values via params.foo and params.bar.

But that makes your component’s design tightly coupled with svelte-spa-router. If I have a component that’s designed to be generally reusable, with parameters like this:

export let foo = "some default foo"
export let bar = "some default bar"

… there doesn’t seem to be a great way to bridge the gap between what is required by svelte-spa-router, and what is expected by general-purpose svelte components.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ItalyPaleAlecommented, Jan 8, 2021

I think this should work, although I haven’t tried it! I would just make two suggestions:

  1. export let params = {} -> Don’t assign default values to that because they will be ignored anyways
  2. I think it’s better to have two lines such as:
    $: foo = params && params.foo
    $: bar = params && params.bar
    
    This is so the reactive statement isn’t triggered if foo or bar change (I’ve seen Svelte having some weird behaviors there)

If your goal is to have a component work in both svelte-spa-router and standalone, you can also simply consider wrapping your component into another one.

For example, assume you have a component Foo that exports proeprties foo and bar. Rather than adding Foo itself to the router (in the route definition object), you create a component called FooRoute that contains this:

<Foo {foo} {bar} />
<script>
export let params
$: foo = params && params.foo
$: bar = params && params.bar
</script>

Then you use FooRoute in the route definition object.

0reactions
ItalyPaleAlecommented, Jan 8, 2021

Well, yes. But it depends on how many components you need wrapped too!

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Top-Level API
React.forwardRef accepts a rendering function as an argument. React will call this function with props and ref as two arguments. This function should...
Read more >
How to render component again at the top level with the new ...
Ok, I can't call setProps , i had to rerender AddTaskForm component in Todo.js . How can I do this in this case?...
Read more >
Top-level await #5501 - sveltejs/svelte - GitHub
I want to have top-level await support for component's <script> that will allow awaiting for promises before component's initialization is ...
Read more >
Props! — and how to pass props to components in React ...
Passing only props from component to component doesn't make the component interactive, because nothing is there to change the props. Props are read-only....
Read more >
How to use Props in React - Robin Wieruch
Everything you need to know about props in React. How to pass props to components, how to set default props, how to know...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found