score:3
Accepted answer
The generateQuestion
callback is asynchronous, so you won't be able to send data in route state via the Link
, but what you can do is move the onClick
handler to the Link
and call event.preventDefault
on the click
event to prevent the navigation action, and issue an imperative navigation after the questions have been fetched.
Example:
import { Link, useNavigate } = 'react-router-dom';
...
const navigate = useNavigate();
...
generateQuestions = async (event) => {
event.preventDefault();
try {
const data = await questionsData();
const arrData = data.map(element => ({
...element,
id: nanoid(),
}));
navigate("/quiz", { state: { questions: arrData } });
} catch(error) {
// handle rejected Promise/error/etc...
}
}
...
<Link to="/quiz" onClick={generateQuestions}>
<button type="button">
Start Quiz
</button>
</Link>
...
In the Quiz
component use the useLocation
hook to access the passed route state.
import { useLocation } from 'react-router-dom';
...
const { state } = useLocation();
const { questions } = state || {};
Source: stackoverflow.com
Related Query
- How to pass props using react router link v6?
- How can I pass props from one component to another using Link and router in react
- How to pass params into link using React router v6?
- how to pass props to the route when using react router <Link>
- I can not get the state from react router Link component using useLocation. So how can I pass it?
- can't pass props using Link in react router dom
- How can I pass function using props in React Router Link?
- How to pass a prop from one component to another when using react router link
- How to pass props inside of Router while using React Context
- how to test props which is pass through Link in react router with React-testing-library or jest
- Can't figure out how to pass props to components when using React Router v5 and Redux
- How can I pass data using <Redirect> in react router v4?
- How to pass the match when using render in Route component from react router (v4)
- How do you pass data when using the navigate function in react router v6
- How to pass data from a page to another page using react router
- React Hook Forms How to pass the errors as a props using Typescript
- How do I pass props and other parameters to function using React Hooks?
- Pass props from react router Link in reactJS
- How to pass props to route in react router v5 with Typescript?
- How to pass props from container component to redux form(component) - react using typescript
- How to pass props to nested routes in React Router 4?
- How to pass props to react component through routing using react-router?
- Using react router, how can I pass component props using a route config?
- How do I pass Meteor Subscription Data into React Component Props using ES6
- React Js: How to pass table row data as props to another page using onClick event
- How to reload a page using react router Link component?
- How to pass a json array that comes from backend to child component using props and react hooks?
- Pass props through react router link typescript
- How to link to certain page upon form submission using React Router
- How do i pass props in react router component?
More Query from same tag
- error TS2307: Cannot find module 'react'
- Is it normal to do an API request from nextjs?
- How to pass variable to Component received in props?
- not found page is not shown when route is not matched
- Individual component exports with Rollup instead of one massive index.js file?
- Why does my styled components transitions are not working?
- using axios in componentWillMount method got me an weired error
- Installed React-Router - The URL changes, but not the view
- Mutating state values in useEffect isn't working (react 16+, react-router v6)
- Keep updating markers on map on top of an overlay image - google maps react
- React-Redux Functional Component Multiple Renders
- Why putting components in different <Route> position in <Switch> can lead to different visual outputs?
- any way to have multiple tabs(screens) in react native with a preview like in a browsers do for example?
- FilePond override server.process on React
- Array of Objects Optimization
- how to take data from redux in index.js
- Why is HOC with styles throwing an error?
- How can I achieve to have a unique key for every <td>
- Setting hook starts infinite loop and app hangs
- How to Post data to database using Fetch API in React.js
- What is the correct way to return map iterator from nested component?
- React Datasheet Grid - Render function does not allow to change
- how to call an API onSubmit in React and update it based on a new value?
- How does KonvaJS handle drawing on the canvas
- Multiple different layout alongside private route
- React function with parameters inside Component/Element
- React Jest test fails to run with ts-jest - Encountered an unexpected token on imported file
- Prop not available in componentDidMount
- Filtering an array with another array and creating new objects?
- Best way to map over asynchronous data in React