Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compose link href from "to" property object with href, query and hash #1118

Closed
marcelmokos opened this issue Feb 13, 2017 · 1 comment · May be fixed by phocks/next.js#1, donavon/next.js#4, NOUIY/next.js#113 or tcotidiane33/next.js#5
Assignees

Comments

@marcelmokos
Copy link

marcelmokos commented Feb 13, 2017

Link element in ReactRouter comes with "to" property which can have href, query object and hash inside.

I prefer to build a query from an object instead of using string templates.

// desired api
// <Link to={{href?: string, query?: object, hash?: string}}>

// desired input
<Link to={{href: "home", query: {key: "value"}, hash: "hash"}}><a>text</a></Link> 

// desired output 
<a href="/home?key=value#hash">text</a>
@marcelmokos marcelmokos changed the title Compose link href from to property object with href, query and hash <Link to={{href?: string, query?: object, hash?: string}}> Compose link href from to property object with href, query and hash Feb 13, 2017
@marcelmokos marcelmokos changed the title Compose link href from to property object with href, query and hash Compose link href from "to" property object with href, query and hash Feb 13, 2017
@timneutkens timneutkens self-assigned this Feb 13, 2017
@timneutkens
Copy link
Member

timneutkens commented Feb 13, 2017

You can use href and as accordingly:

import Link from 'next/link'

<Link href={{href: "home", query: {key: "value"}, hash: "hash"}}>
  <a>text</a>
</Link>

Both href and as accept a url object or string.

import Link from 'next/link'

const urlObject = {href: "home", query: {key: "value"}, hash: "hash"}
<Link href={urlObject} as={urlObject}>
  <a>text</a>
</Link>

If you're using dynamic routes href is the path in the pages directory:

// Will link to /blog/hello-world#hash
import Link from 'next/link'

const urlObject = {href: "/blog/hello-world", hash: "hash"}
<Link href={'/blog/[slug]'} as={urlObject}>
  <a>text</a>
</Link>

@lock lock bot locked as resolved and limited conversation to collaborators May 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.