Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to pass data between components in react

// Its a three step process (react-router-dom is neccessary)

// 1 - you setup component Route in a specific way. Like this
// we will pass data from component1 to component2
import {BrowserRouter as Router, Routes, Route, Link, useParams} from 'react-router-dom';

const App = () => {
  return(
    <Router>
      <Routes>
        <Route path="/component1" element={<Component1 />} />
		<Route path="/component2/:data" element={<Component2 />} />
      </Routes>
    </Router>
  )
}

// setting up component 1
const Component1 = () => {
  let pass = "hello"
  return(
    <div>
      <Link to={"/component2/"+pass}>Click me to pass "pass"</Link>
    </div>
  )
}

// Component 2
const Component2 = () => {
  const { data } = useParams();
  return(
    <div>
      <h2>The data is: {data}</h2>
    </div>
  )
}

// That's all folks
Comment

passing data between components in react js

// Passing data via react-router

// on first component
import React from "react";
import { useNavigate } from "react-router-dom";

const Component1 = () => {
  const navigate = useNavigate();
  return (
    <button onClick={() => navigate("/store", { state: "an object or single value" })} >
    Click Me
    </button>
  );
}

// on 2nd component
import React from "react";
import { useLocation } from "react-router-dom";

const Component1 = () => {
  const data = useLocation().state;
  console.log(data)
  return (
    <div>got the data</div>
  );
}
Comment

pass data between componets in react

import React from 'react'

export default function Parent() {
  return (
    <div>
      
    </div>
  )
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to split by words and punctuation in javascript 
Javascript :: vue style 
Javascript :: enzyme react 
Javascript :: JavaScript setTimeout js function timer 
Javascript :: javascript syntax 
Javascript :: trigger click on first row datatable jquery 
Javascript :: install react-native-safe-area-context 
Javascript :: trigger lambda function on s3 upload code 
Javascript :: setstate in react 
Javascript :: vanilla js http server 
Javascript :: javascript array iteration methods 
Javascript :: input set variable angular 
Javascript :: Parse BSON to JSON 
Javascript :: toggle 
Javascript :: new date javascript invalid date 
Javascript :: get index of first number in string javascript 
Javascript :: unzip array javascript 
Javascript :: JavaScript Initialize Variables 
Javascript :: scroll up 
Javascript :: export json / array to excel in javascript 
Javascript :: js get selected value by id 
Javascript :: javascript name convention 
Javascript :: js how to find max value in an array 
Javascript :: electron js hide on tray icon 
Javascript :: date object js 
Javascript :: how to clear array in javascript 
Javascript :: how to update state in react 
Javascript :: angular ngstyle variable 
Javascript :: uirouter 
Javascript :: spray scala JSON formatter override def read examples 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =