Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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>
  );
}
 
PREVIOUS NEXT
Tagged: #passing #data #components #react #js
ADD COMMENT
Topic
Name
3+6 =