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 :: onSeek video getting paused 
Javascript :: check if a specific user is banned discord js 
Javascript :: Colored tab in react Js MUI 
Javascript :: convert snake case to camelcase javascript recursive 
Javascript :: Listen to custom event in Vue js 
Javascript :: convert array to conventional array js 
Javascript :: vscode react must be in scope when using JSX - eslint 
Javascript :: save input local storage react 
Javascript :: get html from url in react js 
Javascript :: js two operations in ternary 
Javascript :: rpushx redis 
Javascript :: js create an object from another object with some keys removed 
Javascript :: node js swear filter 
Javascript :: node_modules is not generated in docker 
Javascript :: react native avoid keyboard when multiline 
Javascript :: convert string to moment date 
Javascript :: click page object 
Javascript :: Uncaught TypeError: document.getElementById(...).exitFullscreen is not a function 
Javascript :: Check if the same text is repeated javascript todo-app 
Javascript :: with jquery Make a style menu that displays paragraphs and hides them according to the style of the slides 
Javascript :: zustand stores manage loading state 
Javascript :: jquery on mouseover and mouseout 
Javascript :: react onwheel preventDefault 
Javascript :: folder array randomizer 
Javascript :: filter json array based on multiple arguments including lists 
Javascript :: Changing the value of a dropdown when the value of a number box changes in AngularJS 
Javascript :: angularjs Separate values in select containing 2 object 
Javascript :: How to pass React Native Component as a JSON object 
Javascript :: async mutex 
Javascript :: Get value by key from json array 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =