Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

426
Views
React JS how do you pass an array as a parameter using react-router-dom

I am trying to pass an array from / (Home) my /item (itemInfo) page

This is the main App() where we route:

function App() {
  return (
    <Router>
      <div className="Application">
        <Switch>    {/*Makes sure we are only on one route at a time*/}
          <Route exact path='/' component={Home} />
          <Route exact path='/item/:pId' component={ItemInfo} />
          <Route exact path='/vendor' component={VendorInfo} />
          <Route exact path='/addVendor' component={AddVendor}/>
          <Route exact path='/addProduct' component={AddProduct}/>
        </Switch>
      </div>
    </Router>
  );
}

And I have no clue on how to pass the array from {Home} into {ItemInfo}, since we can't pass it as a URL parameter (unlike :pId) This is the {ItemInfo} Page:

import React from 'react'
import { useParams } from 'react-router-dom';    //Helps us redirect to other pages

function ItemInfo(){

    let {pId}= useParams();
    //want to have [data] array on this page to access it hopefully like this 
        //console.log(data) and see the array on the console

    function addNewDataRow(){
        console.log("ADD new Data Row");
    }

    return (
        <div className="Application">
            <header>
                <nav className="navbar navbar-dark bg-dark">
                    <div className="container-fluid">

This is the {Home} (where I want to pass the data array from):

function Home(){

  const product_data=[
    {pId: 1, pName:'product name', pDescription:' description here', pQuantity:1, pWeightType:'Kg'},
    
  ]


  const [data,setData]=useState(product_data);
  const [vData, setvData]=useState(vendor_data);


  const history=useHistory();
  

  function itemTableRowClicked(e){
    console.log(e.target.id);
    console.log("YOU CLICKED ME");
    let path=`/item/:${e.target.id}`;
    //let path=`/item`;
    history.push(path);

  }

The function itemTableRowClicked(e) is the one that re-routes to the ItemInfo Page

I am a fairly new programming student, so let me know if my description isn't clear or I am missing essential information :)

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

React possesses one-way data flow, i.e parent to child . one approach is to define state in a component which is parent to both Home and ItemInfo i.e App but that's not a good approach. Second is to use Context Api or other state management tools like redux, flux etc. reactjs.org/docs/context.html (for context api)

function App() {

  const product_data=[
    {pId: 1, pName:'product name', pDescription:' description here', pQuantity:1, pWeightType:'Kg'},    
  ]
  const [data,setData]=useState(product_data);
  return (
    <Router>
      <div className="Application">
        <Switch>    {/*Makes sure we are only on one route at a time*/}
          <Route exact path='/' >
           <Home data={data} setData={setData} />
          </Route>
          <Route exact path='/item/:pId'>
           <ItemInfo data={data} />
          </Route>
          <Route exact path='/vendor' component={VendorInfo} />
          <Route exact path='/addVendor' component={AddVendor}/>
          <Route exact path='/addProduct' component={AddProduct}/>
        </Switch>
      </div>
    </Router>
  );
}
//Home component
function Home({data,setData})......

//ItemInfo component
function ItemInfo({data}){



about 4 years ago · Juan Pablo Isaza Report

0

You're already using useHistory from react-router. This would allow you to push a state along with the route change. The state is where you can pass the array.

// Push a new entry onto the history stack with a query string
// and some state. Location state does not appear in the URL.
history.push('/home?the=query', { someArray: [...stuff] });

You can then dereference that state in the second component from props.location.state.someArray.

Of course, you'll need to change your ItemInfo component to accept props.

More here: https://v5.reactrouter.com/web/api/history

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!