How can we pass parameter with this.props.history.push('/page') in React-Router v4? It is not ref ,It is variable that pointing to this; this.props.history.push('/template'); take me to next page but i want to pass props with them .ref = this; – IshanGarg May 23, 2017 at 4:39 The component that is rendered by a Route should have access to this.props.location, this.props.history, etc. I think you don't need to use ref anymore with v4. Try doing this.props.history.push('/template'); – Saad May 22, 2017 at 20:04
this.props.history.push({
pathname: '/template',
search: '?query=abc',
state: {
detail: response.data
}
})
So while navigating you can pass props to the history object like
this.props.history.push({
pathname: '/template',
search: '?query=abc',
state: {
detail: response.data
}
})
or similarly for the Link
component or the Redirect
component
<Link to={{
pathname: '/template',
search: '?query=abc',
state: { detail: response.data }
}}> My Link </Link>
Extending the solution (suggested by Shubham Khatri) for use with React hooks (16.8 onwards):
package.json(always worth updating to latest packages)
{
...
"react": "^16.12.0",
"react-router-dom": "^5.1.2",
...
}
Passing parameters with history push:
import {
useHistory
} from "react-router-dom";
const FirstPage = props => {
let history = useHistory();
const someEventHandler = event => {
history.push({
pathname: '/secondpage',
search: '?query=abc',
state: {
detail: 'some_value'
}
});
};
};
export default FirstPage;
While navigating you can pass props to the history object: The search property is used to pass query params in push() method. Made by Michael Sakhniuk
this.props.history.push({
pathname: '/template',
search: '?name=sudheer',
state: {
detail: response.data
},
});
How to pass params with history.push/Link/Redirect in react-router v4? How can we pass parameter with this.props.history.push('/page') in React-Router v4? Passing parameters with history push: This question is tagged with reactjs react-router
.then(response => {
var r = this;
if (response.status >= 200 && response.status < 300) {
r.props.history.push('/template');
});
So while navigating you can pass props to the history object like
this.props.history.push({
pathname: '/template',
search: '?query=abc',
state: {
detail: response.data
}
})
or similarly for the Link
component or the Redirect
component
<Link to={{
pathname: '/template',
search: '?query=abc',
state: { detail: response.data }
}}> My Link </Link>
Extending the solution (suggested by Shubham Khatri) for use with React hooks (16.8 onwards):
package.json(always worth updating to latest packages)
{
...
"react": "^16.12.0",
"react-router-dom": "^5.1.2",
...
}
import {
useHistory
} from "react-router-dom";
const FirstPage = props => {
let history = useHistory();
const someEventHandler = event => {
history.push({
pathname: '/secondpage',
search: '?query=abc',
state: {
detail: 'some_value'
}
});
};
};
export default FirstPage;