score:1
Accepted answer
I think the idea would be to use an effect to scroll to the appropriate component after the component mounts. Perhaps something like this:
React.useEffect(() => {
const anchor = window.location.hash.slice(1);
if (anchor) {
const anchorEl = document.getElementById(anchor);
if (anchorEl) {
anchorEl.scrollIntoView();
}
}
}, []);
Notes:
- I haven't tested this.
- useLayoutEffect instead of useEffect may give a better experience here; see this article for details.
- I'm bypassing React and using the DOM. I usually try to avoid that, but since this logic is isolated and inherently focused on browser DOM behavior, I think this is a good use case for it (instead of complicating the code by pulling in more interactions with your React components.)
- scrollIntoView is convenient but experimental; consider using the scroll-into-view-if-needed NPM package instead.
score:0
I don't know if I understand correctly.
I think, you should make a function for wait everything complete.
but, if you can't catch exactly timing when everything fine, you can use trick.
example :
const CategoryDisplay: React.FC<Props> = (props) => {
...
const [accordionId, setAccordionId] = useState("");
useEffect(()=>{
// this component mounted
...
setTimeout(()=>{
// do something
setAccordionId(`category-${accordion.id}`);
},0);
}, []);
return (
<>
<Accordion
id={accordionId}/>
</>
);
};
This will definitely run in the frame after the component mounted.
Source: stackoverflow.com
Related Query
- ReactJS: Go to anchor link in HTML with react router dom only after the full page load
- ReactJS: Go to anchor link in HTML with react router dom
- React Redux with redux-observable use the router to navigate to a different page after async action complete
- React Router works only after refreshing the page
- How to add Link from react router dom to the submit button with the default Submit validation
- React Router issue. Whenever login button is clicked. I am directed to a blank page with the homepage link appended to the login page link is made
- React JS render the element to the Dom of only one html page in my Django app
- React router 4 - Why is my component only rendered on manually setting the url but not on clicking a Link with same url?
- react router dom not re-rendering after connecting the router component with redux
- React Router - Stay at the same page after refresh
- react router dom link with params dont allways work
- Next.JS - Gist's embedded in markdown rendered via dangerouslySetInnerHTML only show after full page load, not React route load
- React Router V4: How to render a modal in the same screen changing only the url and then with that url, be able to rebuild the whole screen
- React router - pass api data to the linked component to open with a new page
- React Router Link reloading page : Conflict with external event
- How to retrieve the parameter after a hash (#) with React Router and useParams hook?
- React Router doesn't load the new page component after redirecting via navigate
- React Router scroll page to the top after transition
- Disappearing HTML on first visit to the page with Gatsby and Material UI in production only
- Reactjs react router dom useParams not working with dynamic url
- React router dom - Link. How can I put an ID into the path of Link
- React Router Dom Redirect needs to refresh the page to work
- How to make react router Link load page like anchor tag?
- Serving a static html page from a single page react app with react router
- How can I add multiple path for home page with react router dom 6?
- Redirect to a page that only show the contents of a component/page using React Router
- Why it gets the correct data from localStorage only after refreshing the page in React
- How to set active only one link with react router for multiple url state?
- What is the difference between Nav.Link vs Link vs NavLink in react router DOM and react bootstrap?
- React Router Link with params not reloading page with new data from componentDidMount and Redux axios data fetching
More Query from same tag
- Form data Error Cannot set headers after they are sent to the client
- google-map-react: Adding circle radius
- How to share same function with multiple react components?
- react-stripe-elements Error: You must provide a Stripe Element or a valid token type to create a Token
- Pass props to styled-components
- Styling a vanilla html table in react with a style-components
- How to get a onClick to work in a row - reactjs
- React-Router different animation for each route
- In React how to PROPERLY pass input value from child to parent
- Tailwindcss manual dark mode doesn't work in NextJS project
- Custom Active Link in react-router v4
- React.js with Django without using webpack
- React Edit values are not bind in dynamic input select
- Staying DRY with Async Calls in React Redux Application With Redux Thunk
- Using typescript and styled-component when extending third-party component
- UnitTests in React - Method “text” is meant to be run on 1 node. 0 found instead
- react-datepicker fails with moment and new Date
- What is the best pattern for dynamically rendering components?
- How to disable views button in StaticDatePicker in React MUI v5.2.2?
- Unable to redirect after authentication Ionic React private routes
- how to add something like a function or i dont know to use a multiple task on onClick
- Building Pagination in Reactjs
- React: Resizing a div including handlers, different cursors i.e ns-resize - no jquery
- Animate Element in React
- "npm start" command in react app opens up 'nginx' welcome page on browser
- XML request and response with fetch?
- React state update step behind
- Element values shifting and changing before transition with react-transition-group
- firestore is not a function
- What can I use in functional components to have same behavior as componentDidMount?