1
  1. A component called "BasicSearch" is used on all pages.
  2. The search component should be displayed on full screen with a vertical scroll to see the contents with a close button on the right top corner.
  3. The search components have some default links to navigate to different pages.
React.useEffect(() => { document.body.style.position = "fixed"; }, []);
return (
    <div className="container">
        <Button onClick={() => { document.body.style.position = "relative"; }}> close </Button>
        <a href="./xyz">Link1 </a>
        <a href="./xyz-abc">Link2 </a>
    </div>
);

The body element is set to position: relative when the close button is clicked. but, when the user navigates via any of the Link buttons then the body element is unchanged.

Could someone help me understand where I'm going wrong and how to fix this issue?

2 Answers 2

1

Make position state and add dependency to useEffect so that when its changes useEffect will change the body position to new position. And use Link tag of react-router-dom. Actually page reloads when you click on link using anchor tag so your position automatically set to its initial state:

const Search= ()=>{
 
  const [position, setPosition] = useState("fixed"); // initially its fixed
  React.useEffect(() => { document.body.style.position = position }, [position]);

  return (
    <div className="container">
    <Button onClick={() => setPosition("relative")}> close </Button>
    <Link to="/xyz">Link1 </Link>
    <Link to="/xyz-abc">Link2 </Link>
    </div>
  );
}
Sign up to request clarification or add additional context in comments.

8 Comments

Do you have any preference if I use a component instead of <link>, then I should pass the state as props and handle this inside the linking component?
Its a good practice and recommendable to use Link Component (react-router-dom) in single SPA to access different pages.
Ok, When clicking the link, for example, it won't change the body element again to a relative - it will only navigate to another page (Without refreshing the page). So how to handle the position when clicking the link?
On Link Switch you want to change position to relative ? on all pages or some pages?
Ok then pass onClick to Link component like: <Link to="/xyz" onClick={() => setPosition("relative")}>Link1 </Link>
|
0

Use a state for handling the position, to make sure that React will update the DOM, in combination with your useEffect:

const Search= ()=>{
  const [position, setPosition] = useState("fixed");
  useEffect(() => { document.body.style.position = position }, [position]);
  return (
    <div className="container">
        <Button onClick={() => setPosition("relative")}> close </Button>
        <a href="./xyz">Link1 </a>
        <a href="./xyz-abc">Link2 </a>
    </div>
  );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.