I have a problem with navigation between two routes of same component.
I have a page where there is a form to create and edit user. The routes are:
/user
/user/:id
But when try navigate from /user/:id to /user to create new user, the states of component not redeclared. The states come with the same value of page /user/:id. This is occur only when navigate to same component.
See routes:
<Route path='user/:id' element={<UserForm />} />
<Route path='user' element={<UserForm />} />
The component:
const UserForm = () => {
const navigate = useNavigate();
const params = useParams<FormParams>();
// const [searchParams, setSearchParams] = useSearchParams();
const [loading, setLoading] = useState<boolean>(true);
const [message, setMessage] = useState<MessageProps>();
const [data, setData] = useState<UserModel>(newData);
...
}
export default UserForm;
Redirect Function:
const newForm = () => {
try {
debugger;
navigate('/user');
} catch (error) {
console.log("ERROR");
}
}
If I refresh page, the states is redeclared. As it should happen in the redirect.