React Query String Works in local but redirects to 404 in production

Description:
I have a React application with a route setup to display a public profile using parameters like /profile?id=test123 or /?profile=test123. I’ve implemented the route using useSearchParams() and useParams() hooks.

What I’ve Done:

  • Created a route using useSearchParams() and useParams() hooks.
  • Implemented fetching data based on the parameters provided.

Issue:

  • The setup works perfectly in my local environment, and data is fetched correctly.
  • However, in the production environment, it redirects to a 404 page.

Code Snippets:

// PublicProfile.jsx
import { useEffect, useState } from 'react';
import { useSearchParams } from 'react-router-dom';
import { publicprofile } from '../../../../API/APIs';
import {
  Typography,
  List,
  ListItem,
  ListItemText,
  Divider,
  Box,
} from '@mui/material';

function PublicProfile() {
  let [searchParams, setSearchParams] = useSearchParams();
  console.log(searchParams);
  const logSearchParams = () => {
    console.log(searchParams.get("id"));
  };
  logSearchParams();
  const [school, setSchool] = useState(null);
  useEffect(() => {
    const fetchSchoolData = async () => {
      try {
        const payload = {
          query: searchParams.get("id"),
        };
        const response = await publicprofile(payload);

        if (!response.ok) {
          throw new Error('Failed to fetch school data');
        }
        const data = await response.json();
        setSchool(data);
        console.log(data);
      } catch (error) {
        console.error('Error fetching school data:', error);
      }
    };

    fetchSchoolData();
  }, [searchParams.get("id")]);

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

  return (
    <Box p={2} border={1} borderRadius={4}>
      <Typography variant='h2'>{school.name}</Typography>

      <Box mt={2}>
        <Typography>
          <strong>Address:</strong> {school.address}
        </Typography>
      </Box>

      <Divider variant='middle' />

      <Box mt={2}>
        <List>
          <ListItem>
            <ListItemText
              primary={<strong>Phone:</strong>}
              secondary={school.contactDetails}
            />
          </ListItem>
        </List>
      </Box>

      <Divider variant='middle' />

      <Box mt={2}>
        <Typography>
          <strong>Established Date:</strong> {school.establishedDate}
        </Typography>
      </Box>

      <Divider variant='middle' />

      <Box mt={2}>
        <Typography>
          <strong>Facilities:</strong> {school.facilities.join(', ')}
        </Typography>
      </Box>
      {/* Add more details as needed */}
    </Box>
  );
}

export default PublicProfile;

Route Structure:

// Routes.js
<Route path='/' element={<Layout />}>
  <Route path='/' element={<LandingPage />} />
  <Route path='/register' element={<Register />} />
  <Route path='/login' element={<Login />} />
  <Route path='/forgotpassword' element={<ForgetPassword />} />
  <Route path='/validateOtp' element={<OTPValidation />} />
  <Route path='/resetpassword' element={<ResetPassword />} />
  <Route path='/profile' element={<PublicProfile />} />
</Route>
<Route path='/admin/' element={<AdminLayout />}>
  <Route path='/admin/profile' element={<AdminProfile />} />
  <Route path='/admin/school' element={<SchoolInfo />} />
  <Route
    path='/admin/createclassroom'
    element={<CreateClassroomForm />}
  />
  <Route
    path='/admin/generateInviteCode'
    element={<GenerateInviteCode />}
  />
  <Route path='/admin/createNewSchool' element={<CreateSchool />} />
  <Route path='/admin/attendance' element={<ViewAttendance />} />
</Route>

Question:
I’m trying to understand why this setup works in my local environment but redirects to a 404 page in the production environment. Any insights into what might be causing this issue would be greatly appreciated. Thank you!

I’m trying to understand why this setup works in my local environment but redirects to a 404 page in the production environment. Any insights into what might be causing this issue would be greatly appreciated. Thank you!

And I expect solution for how to use query params in react on production.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật