如何将页面移动到 'page load' 上的锚点 link 部分?

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

我可以这样检测锚点变化:

/滚动到部件:

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);

我在这个组件中插入了我的 App

 <ScrollToPart>
   <App />
 </ScrollToPart>

在应用程序的一个组件中,这是列表,其中有

副标题:

    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; 

所以现在../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;

使用ScrollToPart我可以检测到锚点的变化link我可以移动到那个特定的部分。 (我有一些分页功能,可以将我导航到 'my-list' 和 'team-list')。

当我的页面加载时,我无法移动到任何一个部分。 当页面 loads/reloads.

时,elementScrollToPart 组件中作为 null 传递

你能帮忙吗?

答案在这里,

只需设置定时器,直到它找到元素。 https://github.com/ReactTraining/react-router/issues/394#issuecomment-141526205

希望对您有所帮助