React js code problem #159487
-
BodyAfter submitting the form to add a contact, the contacts list does not update on the ContactsPage. It seems that the updated contacts state from App.js is not passed as props to ContactsPage. Files to check: App.js — state management and passing props to ContactsPage via router ContactsPage.js — receiving and using the contacts prop Guidelines
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
|
The problem is in the way contacts are being passed to the ContactsPage component through React Router. Your use of useEffect confirms that the contacts state is updating correctly in App.js (you see the log "✅ contacts изменились"), but the ContactsPage isn't re-rendering with the new data. check this solution can help : const router = createBrowserRouter(createRoutesFromElements(
<Route path="/" element={ <Root/> }>
<Route index element={ <Navigate to={ROUTES.CONTACTS} replace/> }/>
<Route
path={ROUTES.CONTACTS}
element={ <ContactsPage addContact={addContact} /> }
loader={() => ({ contacts })}
/>
<Route
path={ROUTES.APPOINTMENTS}
element={ <AppointmentsPage addAppointment={addAppointment} /> }
loader={() => ({ contacts, appointments })}
/>
</Route>
)); |
Beta Was this translation helpful? Give feedback.
-
I fixed the way contacts were passed into ContactsPage, making sure the props flow properly. Also tweaked the duplicate check so it actually reacts to updates. The submit handler now clears the form right after adding a new contact, which it wasn’t doing before. I threw in some console.logs too, so it’s easier to see what’s going on while testing. Overall, just cleaned things up so the contact list updates like you'd expect. I hope this works for you. |
Beta Was this translation helpful? Give feedback.
-
|
Hi there @vanaver! I ran your code and noticed the issue lies in how the router is being set up inside the App component. You're creating a new router instance on every render by calling createBrowserRouter inside the function body. This results in React Router remounting components on every render, which can cause unexpected behavior like state resets and lost navigation history. To fix this, you should move the router definition outside of the component, or better yet, use the standard BrowserRouter with Routes, which offers a more stable and idiomatic setup. Here’s a simplified and recommended way to structure your App.js using BrowserRouter: import React, { useState, useEffect } from "react";
import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
import Root, { ROUTES } from "./components/root/Root";
import { AppointmentsPage } from "./containers/appointmentsPage/AppointmentsPage";
import { ContactsPage } from "./containers/contactsPage/ContactsPage";
function App() {
/*
Define state variables for
contacts and appointments
*/
const [contacts, setContacts] = useState([]);
const [appointments, setAppointments] = useState([])
useEffect(() => {
console.log("✅ contacts изменились:", contacts);
}, [contacts]);
/*
Implement functions to add data to
contacts and appointments
*/
const addContact = (name, phone, email) => {
const contact = { name, phone, email };
console.log(contact)
setContacts((prev) => [...prev, contact]);
};
const addAppointment = (title, contact, date, time) => {
setAppointments((prev) => [...prev, {title, contact, date, time }])
};
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Root />}>
<Route index element={<Navigate to={ROUTES.CONTACTS} replace />} />
<Route
path={ROUTES.CONTACTS}
element={
<ContactsPage
contacts={contacts}
addContact={addContact}
/>
}
/>
<Route
path={ROUTES.APPOINTMENTS}
element={
<AppointmentsPage
appointments={appointments}
addAppointment={addAppointment}
contacts={contacts}
/>
}
/>
</Route>
</Routes>
</BrowserRouter>
);
}
export default App;This approach avoids the rerender issue and follows best practices for routing in React. Hope this helps! 🙌 |
Beta Was this translation helpful? Give feedback.

Hi there @vanaver! I ran your code and noticed the issue lies in how the router is being set up inside the App component.
You're creating a new router instance on every render by calling createBrowserRouter inside the function body. This results in React Router remounting components on every render, which can cause unexpected behavior like state resets and lost navigation history.
To fix this, you should move the router definition outside of the component, or better yet, use the standard BrowserRouter with Routes, which offers a more stable and idiomatic setup.
Here’s a simplified and recommended way to structure your App.js using BrowserRouter: