最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create routes with params in gatsbyjs - Stack Overflow

programmeradmin5浏览0评论

I want to create a route that uses a slug as a parameter in my gatsby generated website.

I have a list of projects that sit on the route /projects/<slug>.

Usually with react router I would create a route like so:

<Route exact path='/projects/:project' ponent={Projects} /> 

It seems in gatsby, I have to create a new file under the ./pages directory and that creates a new route. I have a page called projects where I try do a look up on the route param but only seem to get the 404 page.

// ./pages/projects.js

class SingleProject extends Component {

  state = {
    project: {}
  }

  ponentDidMount(){
    const project = this.props.projects.find(project => project.slug === this.props.match.params.project)
    this.setState({project})
  }

  render() {
    return (
      <div className="single-project" >
      </div>
    )
  }
}

export default SingleProject;

How can I have routes with parameters in gatsby?

I have just e across client only routes but I guess these routes wont be statically generated.

I will have a predefined list of slugs so perhaps there is a way to create a page for each of the project slugs? I guess I could manually create a file within ./pages/projects/<slug> foreach project I have?

I want to create a route that uses a slug as a parameter in my gatsby generated website.

I have a list of projects that sit on the route /projects/<slug>.

Usually with react router I would create a route like so:

<Route exact path='/projects/:project' ponent={Projects} /> 

It seems in gatsby, I have to create a new file under the ./pages directory and that creates a new route. I have a page called projects where I try do a look up on the route param but only seem to get the 404 page.

// ./pages/projects.js

class SingleProject extends Component {

  state = {
    project: {}
  }

  ponentDidMount(){
    const project = this.props.projects.find(project => project.slug === this.props.match.params.project)
    this.setState({project})
  }

  render() {
    return (
      <div className="single-project" >
      </div>
    )
  }
}

export default SingleProject;

How can I have routes with parameters in gatsby?

I have just e across client only routes but I guess these routes wont be statically generated.

I will have a predefined list of slugs so perhaps there is a way to create a page for each of the project slugs? I guess I could manually create a file within ./pages/projects/<slug> foreach project I have?

Share Improve this question edited Aug 23, 2018 at 15:31 Stretch0 asked Aug 23, 2018 at 15:12 Stretch0Stretch0 9,26315 gold badges92 silver badges157 bronze badges 1
  • Question name is misleading, as it is confused with Query parameters, maybe use variable instead: en.wikipedia/wiki/Query_string – Ambroise Rabier Commented Sep 3, 2020 at 13:07
Add a ment  | 

1 Answer 1

Reset to default 13

You'll want to use the createPage method that Gatsby gives you inside gatsby-node.js's createPages API. There is a guide in the Gatsby documentation that shows you can achieve exactly this. Here's an even simpler example from a similar question.

export const createPages = ({ actions }) => {
  const { createPage } = actions;

  createPage({
    path: '/projects/hello-world',
    ponent: SingleProject,

    // Send additional data to page ponent
    context: {
      id: 'hello-world',
    },
  });
};
发布评论

评论列表(0)

  1. 暂无评论