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

277
Views
How to pass JSON data using useNavigation Hooks in React Js?

This is my Json file which I created in my app.

export const Data = [
    {
        id: 1,
        title: "Tilte 1",
        description: "Decription 1 Data",
    },
    {
        id: 2,
        title: "Tilte 2",
        description: "Decription 2 Data",
    }
];

This is my main file from where I navigate it. I use json file to display all the records on page. When I click on selected item it will get its id and navigate to another page, where i can get the data of selected item coming from json.

import React from "react";
import { Data } from "./JSON"
import { useNavigate } from 'react-router-dom'

const Home = () => {
    let naviagte = useNavigate();
    return (
<>


         {Data.map((data, key) => {
            return (
            <div class="card" >
                <div class="card-body">
                    <h5 class="card-title" key={key.id}>{data.title}</h5>
                    <p class="card-text">{data.description}</p>
                    <button onClick={() => naviagte(`/service/${data.id}`)}>{data.title} </button>
                </div>
            </div>
            );
          })}
     </>

    )
}
export default Home;

When I navigate to another page where I want to display all data regarding the selected id. It shows only id not all data.

import React, {useState, useEffect} from "react";
import { Data } from "../home/JSON"
import { useParams } from "react-router-dom";

const Service = () => {

    const { id } = useParams();

    const [data, setData] =useState('');
    console.log("check", data);
  
     useEffect(() => {
      setData (Data.map((_data) => _data.id === id ))
     }, [id])


    return(
        <>
{id}
{data.title}
{data.description}
        </>
    )
}

export default Service;

Please guide me what I miss here. Thanks in Advance

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

0

Since you are importing the data in both places you just need to find the data by the id property instead of mapping it to booleans. Keep in mind that your id property is a number but the id route param will be a string, so you will need to convert them to a compatible type for the strict equality (===) check.

Example:

useEffect(() => {
  setData(Data.find((_data) => String(_data.id) === id));
}, [id]);

Since data is treated as an object in the render return you'll want to insure you maintain a valid state invariant. Update the initial data state to be an object, and check that Array.prototype.find returned a defined object from the Data array before updating state.

const Service = () => {
  const { id } = useParams();

  const [data, setData] = useState({});
  console.log("check", data);
  
  useEffect(() => {
    const data = Data.find((_data) => String(_data.id) === id);   
    if (data) {
      setData(data);
    }
  }, [id]);

  return (
    <>
      {id}
      {data.title}
      {data.description}
    </>
  );
};
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!