score:2
sending data with route transition
you can send state along with the route transition using a link
's to object.
to: object
an object that can have any of the following properties:
- pathname: a string representing the path to link to.
- search: a string representation of query parameters.
- hash: a hash to put in the url, e.g. #a-hash.
- state: state to persist to the location. <-- you need this
array.map((each)=>(
<link
to={{
pathname: `details/${each.id}`,
state: {
// whatever you need to send with the route transition
},
}}
>
<card props={each}/>
</link>
))
receiving the route state
you've a few options.
easiest and more common way now with using the uselocation react hook in functional components.
const { state } = uselocation();
decorating a component with the withrouter higher order component and inject route props.
if your component is decorated with
withrouter
it will have the current route props injected as props.export default withrouter(eachcomponent);
access
state
from thelocation
prop.const { state } = this.props.location;
directly render by a
route
component and receive the route props.if your component is rendered directly by a
route
it will have the route props passed to it.<route path="/details/:id" component={eachcomponent} />
access
state
from thelocation
prop.const { state } = this.props.location;
note: route state is transient, meaning it only exists during the transition from one route to the next. if you navigate directly to a receiving route it won't have the state because it didn't use the transition from the route providing the state.
score:2
you can pass data with link using location state or search (or query params):
<link
to={{
pathname: '/path/to/my/component',
search: 'id=123&name=foo',
state: { data: 111 },
}}
>
click me
</link>
and to retrieve in the mycomponent
(e.g.using hooks):
const location = uselocation()
const query = new urlsearchparams(location.search)
console.log(location.state)
console.log(query.get('id'))
console.log(query.get('name'))
ps: you can also make a custom hook - usequery
for query params.
but be aware that if you open path - /path/to/my/component
directly in the browser by typing it manually, there will be no location state or query params.
and urlsearchparams is not supported in internet explorer, if you need to support ie as well, you can use some package like query-string.
Source: stackoverflow.com
Related Query
- React router - pass api data to the linked component to open with a new page
- How to pass data to another component not in the URL with React Router
- How to fetch the new data in response to React Router change with Redux?
- React Router doesn't load the new page component after redirecting via navigate
- React Router "Link to" does not load new data when called from inside the same component
- React router dom passing data from parent component to child router component does not pass the props.match
- React Router Link with params not reloading page with new data from componentDidMount and Redux axios data fetching
- React Router Dom, pass data to a component with useNavigate
- React Redux, load data from the API on first page load ONLY. Render component once data is loaded
- I have spent the last 8 hours trying to pass a simple JSON object with some data from an api through my express backend to one of my react components
- How to open a new page and pass data between them in React JS
- How to pass open weather API data to React front-end with serverless function and axios
- How do I use react context API to pass data from parent component to child component when using react-router in the parent component in React.js?
- How to load a page with the data from a component in react
- react router / i wanna go to a new page instead of adding the component under the mother component
- How to make a nested react router component load to a new page and not half-way down the page
- How to pass data from one page to another with react router dom Link?
- How to pass the API response data to another react page
- How to mock history.push with the new React Router Hooks using Jest
- Is there a right way to pass data into a React component from the HTML page?
- How should the new context api work with React Native navigator?
- Passing a function with React Context API to child component nested deep in the tree
- How to pass the match when using render in Route component from react router (v4)
- Laravel 5.5 render a React component in a blade view with data from the controller
- onEnter Transitions with React Router and Redux Simple Router Dont Render New Route's Component
- How do you pass data when using the navigate function in react router v6
- Is it possible to access the current route component with React Router
- How to pass data from React Form -> Flask Backend -> React Component (does it have something to do with CORS)?
- React Router - Go back to the last page visited, and not the default component
- React Hook useEffect : fetch data using axios with async await .api calling continuous the same api
More Query from same tag
- ReactJs: How to change color of multiple divs together using if else
- React onClick() doesn't trigger when inside map function
- How to stretch width of a Dialog box of material ui in reactjs?
- How does react hooks handles callback when it sets the state?
- How to select every two / three children or sibling elements and apply styling?
- Can't use mobs decorator, error:Support for the experimental syntax 'decorators-legacy' isn't currently enabled
- How to set and handle language in react-router from?
- Print divs on same line height css
- Redux-Form Field-Level Validation: Why aren't the error messages showing?
- React Deployment in Azure DevOps
- How pass a prop in my route from my component to a child component with an 'onClick'
- How to automatically get and send User's geolocation values as a POST request to the backend
- React async function is not a function
- React JS get current date
- In react JS fields losing focus after first onChange
- How do I make several arrays inside one based on relationships?
- Typescript error: 'List<Element>' is missing the following properties from type 'ReactElement<any, any>': type, props, key
- Material UI DateRangePicker disable dates + 7 days
- React state updates without me calling setState?
- Express: serve Webpack bundle on subroutes
- TypeError: axios__WEBPACK_IMPORTED_MODULE_1___default.a.get(...).than is not a function
- Common Delete Modal Component in react with Reactstrap
- How can i apply map function to update all element of JSON body
- React - React.createElement: type should not be null, undefined, boolean
- copy-webpack-plugin doesn't copy files
- Using Electron showOpenDialog Filter allow to type user *.* which fails the filter
- Equivalent of Ahead Of Time Compilation in React
- You may need an appropriate loader to handle this file type. with webpack
- React Server Components Performance on SEO
- Heroku - React application is calling localhost in fetch() requests instead of the Heroku URL for my Express backend