Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to pass data between routes in react

// React Router v6
// pass data between routes
// ---------------------------------------------------------------------
// sender.js/jsx
import { useNavigate } from "react-router-dom";

const navigate = useNavigate();
navigate('/other-page', { state: { username: 'user', password: '696' } });
// ---------------------------------------------------------------------
// receiver.js/jsx
import { useLocation } from "react-router-dom";

const location = useLocation();
console.log(location.state) // gives: {username: 'user', password: '696'}
Comment

send data through routes in react

const navigate = useNavigate();
navigate('/other-page', { state: { id: 7, color: 'green' } });
Comment

send data through routes in react

const {state} = useLocation();
const { id, color } = state; // Read values passed on state
Comment

react router how to send data

props.history.push('/other-page', { id: 7, color: 'green' }))

Then, you can access the state data in '/other-page' via:

props.location.state
Comment

passing data in route react

<Route path="/" component={() => <Search name={this.props.name} />} /> 
Comment

passing data in route react

<Route path="/:name" component={Search} />
Comment

passing data in route react

<Route path="/" render={() => <Search name={this.props.name} />} /> 
Comment

passing data in route react

render={routeProps => <Search name={this.props.name} {...routeProps} />}
Comment

PREVIOUS NEXT
Code Example
Javascript :: discord js send dm to user 
Javascript :: js get anchor 
Javascript :: package json proxy 
Javascript :: alias import javascript 
Javascript :: json fetch data doest show 
Javascript :: mongoose and express get requests 
Javascript :: javascript sort object by key 
Javascript :: javascript select all divs with class 
Javascript :: p5.js change button position 
Javascript :: between in mongodb 
Javascript :: react native run android 
Javascript :: javascript random sort array 
Javascript :: js decrement for loop 
Javascript :: first letter capital in javascript 
Javascript :: validate age javascript 
Javascript :: get minutes and seconds from seconds 
Javascript :: vuex-module-decorators access other state 
Javascript :: js cypress div text 
Javascript :: compare dates in js 
Javascript :: number to binary javascript 
Javascript :: s3 list objects in folder node js 
Javascript :: remove milliseconds from datetime js 
Javascript :: javascript paragraph count 
Javascript :: google maps init map 
Javascript :: node wrangler preview 
Javascript :: last element of an array javascript 
Javascript :: how to redirect to another page in javascript on submit type 
Javascript :: vue print date 
Javascript :: toggle classname onclick react 
Javascript :: how to go to next page on button click js 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =