javascriptreactjsreact-routeranchor-scroll

How to move the page to the anchor link portion on 'page load' ?


I can detect the anchor change like this:

/ScrollToPart:

import React from 'react';
import { withRouter } from 'react-router-dom';

class ScrollToPart extends React.PureComponent {

 componentDidMount() {
   // I want to scroll to the view.
   this.scroll();
 }

 componentDidUpdate(prevProps) {
  // Scroll when location changes.
  if (this.props.location !== prevProps.location) {
    this.scroll();
   }
  }

 scroll() {
   // Get the '#' id from the location 
   const id  = (
     this.props.location && this.props.location.hash
   ) ? this.props.location.hash : null;
    if (id) {
       element = document.getElementById(id.split('#').join(''));
       // If element present, scroll me to that part 
       if (element) {
         element.scrollIntoView();
       } else {
         // If element not present, scroll me to the top
         window.scrollTo(0, 0);
       }
     } else {
       // In no anchor element, scroll me to the top
       window.scrollTo(0, 0);
     }
   }

  render() {
    return this.props.children;
  }
 }
 export default withRouter(ScrollToPart);

I inserted my App in this component this:

 <ScrollToPart>
   <App />
 </ScrollToPart>

In one of the components in the App, This is the List and which has

Subtitle:

    import PageSubtitle from '../PageSubtitle';
    import React from 'react';
    import { Link } from 'react-router-dom';

    class EmailList extends React.PureComponent {

      // This function is to get the existing Query parameters
      // and keep them as it is.
      getQueryParameterPath = () => {
        const { location } = this.props;
        const params = new URLSearchParams(location.search);


        let path = '/dashboard/emails?';
        if (params.get('my-page') && params.get('my-page').toString() !== '') {
          path = path + '&my-page=' + params.get('my-page');
        } 
        if (params.get('team-page') && params.get('team-page').toString() !== '') {
          path = path + '&team-page=' + params.get('team-page');
        }

        return path
      }

      render() {
        // This is the 'id' to which I need to move. It's just a 
        // string in its parent component.  
        // listId = 'my-list' and other time listId = 'team-list'
        const { listId } = this.props;

        return (
          <div id={listId}>
            <PageSubtitle 
              inline={true} 
              component={Link}
              to={`${this.getQueryParameterPath()}#${listId}`}
            >
              Title
            </PageSubtitle>
          </div>
        );
      }
    }

    export default EmailList; 

So now ../PageSubtitle

    import PropTypes from 'prop-types';
    import React from 'react';
    import Typography from '@material-ui/core/Typography';


    const PageSubtitle = ({ children, ...other }) => (
      <Typography
        variant="h6"
        {...other}
      >
        {children}
      </Typography>
    );

    export default PageSubtitle;

Using ScrollToPart I can detect the changes with the anchor link I can move to that particular part. (I have some pagination feature, which navigates me to the 'my-list' and 'team-list').

I am not able to move to either of the sections when my page loads. The element is being passed as null in the ScrollToPart component when page loads/reloads.

Can you please help?


Solution

  • Here is the answer,

    just set the timer, until it finds the element. https://github.com/ReactTraining/react-router/issues/394#issuecomment-141526205

    Hope this helps