score:0
for getting location's state in functional component without route props or withroute
hoc, you would use uselocation
instead:
import { usehistory } from "react-router-dom";
const main= () => {
const location = uselocation();
console.log(location);
};
and to pass multiple variables through location state, use object:
history.push("/page1", { pathname: "test", anotherparam: "test2" });
note that: the second param of history.push
is already the state, so you don't need to name it a state
live example:
score:1
you may want to try the object version of the push:
function handlesubmit(event) {
event.preventdefault();
history.push({
pathname: "/main",
state: 'test',
});
};
please also ensure the receiving component is also receiving route props.
const main= (props) => {
console.log(props.location.state);
};
if it not then ensure access to the location
object via the uselocation
hook or the withrouter
higher order component to have them injected as props.
Source: stackoverflow.com
Related Query
- How do I pass multiple parameters using history.push?
- How to pass multiple parameters as an object in url using react
- React how to pass the id to router using history push
- How to pass multiple parameters to input's onChange handler
- Next js - How to pass multiple parameters in url for api?
- how to pass through parameters using a get/ fetch statement (express)
- How can i pass parameters on redirection using react router?
- How to pass params into history push
- How do I pass props and other parameters to function using React Hooks?
- ReactJS - Can't obtain parameters from url using Route. How can I pass params through url with Route?
- How to push route in history using redux-saga
- how to pass multiple parameters to the web API?
- How to pass input value to form onSubmit without using state in component that renders multiple forms?
- How can I pass multiple parameters through a React <select> component?
- How to pass dynamic as well as static parameters {hybrid field variables and functions} in React function using map?
- how to pass parameters to component using react route link
- How to pass optional parameters while omitting some other optional parameters without using undefined and empty ("")
- How to push history to a specific component by using async fetch in react js
- How to push to History in React Router v4?
- How to add multiple classes in Material UI using the classes props?
- How to create multiple page app using react
- How to pass params into link using React router v6?
- How can I pass data using <Redirect> in react router v4?
- using css modules in react how can I pass a className as a prop to a component
- how to update multiple state at once using react hook react.js
- How to implement multiple checkbox using react hook
- How to pass multiple states through react context api
- How to pass props to a styled component in emotion? Using TypeScript
- How to pass the match when using render in Route component from react router (v4)
- How to make multiple Axios requests using a dynamic array of links in Javascript?
More Query from same tag
- Gatsby 3rd party scripts not working as intended
- React table re-renders, losing focus when input text in table cell
- Check if value is negative and apply CSS in React JS
- react-router location did not match any routes
- How to put new object into array of objects in state
- NextJS - Call js function when onClick
- align button right reactjs
- Why is redux state not mapped to react state?
- How to works setState function?
- How to redirect a link using History to a page
- Why are css classes are missing in production when using Tailwind and next.js?
- How shallow prop compare works for connected components?
- How To Toggle Message Using React (Test-Dome Solution1)
- Webpack Dev Server with React Hot Loader
- Get Location on component mount and pass to App State in Ionic React
- setState is not merging the values
- Disable TouchableHighlight onPress?
- React useState toggle single button
- React - Why Props do not update
- How do you export react library component with hooks to use in html file?
- Updated State not being retrieved by method
- Webpack error: Can't resolve 'fbjs/lib/EventListener'
- Portrait Only View In React on Mobile Devices?
- How can I use the built in Object.assign in react?
- How does webpack pick a relative path inside node_modules ? does it reference package.json at all?
- how to set min height in semantic-ui react
- React Router redirect hash link
- Converting stateful React component to stateless functional component: How to implement "componentDidMount" kind of functionality?
- react / redux - How to dispatch an action on componentDidMount
- Google Maps API - How to center marker and map