skip to Main Content

PropertyDetails.jsx

import React, { useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import axios from 'axios';
import '../styles/propertydetails.css';
import Navbar from '../components/Navbar';
import Footer from '../components/Footer';

function PropertyDetails() {
  const { id } = useParams();
  const [propertyData, setPropertyData] = useState(null);

  useEffect(() => {
    // Fetch property details for the given propertyId using Axios
    axios.get(`http://127.0.0.1:8000/api/v1/core/detail/${id}/`)
      .then((response) => {
        setPropertyData(response.data.results); 
      })
      .catch((error) => {
        console.error('Error fetching property details:', error);
      });
  }, [id]);

  if (!propertyData) {
    return <div>Loading...</div>;
  }

  return (
    <div>
      <Navbar />
      <div className="property-details-container">
        <img src={propertyData.main_photo} alt={propertyData.title} className="main-photo" />
        <h2>{propertyData.title}</h2>
        <p>{propertyData.description}</p>
        <p>Price: ${propertyData.price}</p>
        <p>Bedrooms: {propertyData.bedrooms}</p>
        
      </div>
      <Footer />
    </div>
  );
}

export default PropertyDetails;

App.js

import { Routes, Route } from "react-router-dom"
import HomePage from "../src/pages/HomePage"
import SalesPage from "../src/pages/SalesPage"
import RentalsPage from "../src/pages/RentalsPage"
import PropertyDetails from "../src/components/PropertyDetails"
import "./App.css"

function App() {
  return (
    <div className="App">
      <Routes>
        <Route path="/" element={ <HomePage/> } />
              <Route path="sales" element={<SalesPage />} />
              <Route path="rentals" element={<RentalsPage />} />
              <Route path="/property/:id" element={<PropertyDetails />} />
          </Routes>
    </div>
  )
}

export default App

I’ve verified that the id parameter is correctly extracted using useParams. However, I’m still encountering this error. Can someone please help me understand what might be causing this issue and how I can resolve it? Thank you!

I am fetching data from a django-rest api

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution. I resolved the error appear by changing this section

    useEffect(() => {
        // Fetch property details for the given propertyId using Axios
        axios.get(`http://127.0.0.1:8000/api/v1/core/detail/${id}/`)
          .then((response) => {
            setPropertyData(response.data.results); 
          })
          .catch((error) => {
            console.error('Error fetching property details:', error);
          });
      }, [id]);
    

    to

    useEffect(() => {
        // Fetch property details for the given propertyId using Axios
        axios.get(`http://127.0.0.1:8000/api/v1/core/detail/${id}/`)
          .then((response) => {
            setPropertyData(response.data); 
          })
          .catch((error) => {
            console.error('Error fetching property details:', error);
          });
      }, [id]);
    

    changed setPropertyData(response.data.results) to setPropertyData(response.data)


  2. <Route path="/property/:id" element={} />
    In this, you are not sending anything in id, and you are expecting useParams to get some value of id from params.

    As per my knowladge "/property/:id" is not used in React.
    If you want to send something to params, use useNavigate of react-router.

    From parent : navigate(‘/createPost’, { state: { postId: postData?._id, title: postData?.title, content: postData?.content } })

    From Child to get the value :
    import { useLocation } from "react-router-dom";
    const { state } = useLocation()

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search