Answered
The URL for the page contains data like: ?id=123&name=billy-bob
.
How do I grab those values from the URL and make them available to the page via the this.props
object? This should be done in the getInitialProps()
function, right?
You can do this quite easily with built-in Next.js tools.
Here's what your getInitialProps()
function can look like:
static async getInitialProps ({ query }) {
const id = query.id
const name = query.name
return {
id: id,
name: name
}
}
The query
object will contain each query string parameter from the URL.
{
id: "123,
name: "billy-bob
}
And each of those values will be available in your page via the this.props
object.
this.props.id
this.props.name
Note that each query parameter will be a string
.