Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent of this.props.history.push in gatsby?

Tags:

reactjs

gatsby

I'm trying to do this.props.history.push("/payment/" + stripe_plan_id)

What is the equivalent of this.props.history.push in Gastby?

I get the error TypeError: Cannot read property 'push' of undefined

like image 749
Pota Onasys Avatar asked Nov 03 '19 05:11

Pota Onasys


People also ask

What is Gatsby link?

For internal navigation, Gatsby includes a built-in <Link> component for creating links between internal pages as well as a navigate function for programmatic navigation. The component is a wrapper around @reach/router's Link component that adds useful enhancements specific to Gatsby.

Is Gatsby and react the same?

React is a library that is meant to provide a certain set of core functionality for developers to leverage. It is intended to be lightweight and broadly applicable. Gatsby, on the other hand, is a static progressive web app (PWA) generator that offers code and data splitting out of the box.

How do you get the URL for Gatsby?

You can add this as a siteURL property on siteMetadata in gatsby-config. js . Once you have added siteURL , you can form the absolute URL of the current page by retrieving siteURL and concatenating it with the current path from location . Note that the path starts with a slash; siteURL must therefore not end in one.

How do you use the routing in the Great Gatsby?

Routes can be created in three ways: By creating React components in src/pages. By using the File System Route API to programmatically create pages from GraphQL and to create client-only routes. By implementing the API createPages in your site's gatsby-node.


2 Answers

The top answer doesn't work anymore But this does work :

import { navigate } from "gatsby"  
navigate(`/payment/${stripe_plan_id.id}`)
like image 130
Mahdi Faraji Avatar answered Oct 24 '22 23:10

Mahdi Faraji


Gatsby uses reach router. Try the following:

import { navigate } from "@reach/router"  

navigate(`/payment/${stripe_plan_id.id}`)
like image 34
ksav Avatar answered Oct 24 '22 22:10

ksav