如何将页面移动到“页面加载”上的锚链接部分?

时间:2019-01-07 18:28:15

标签: javascript reactjs react-router anchor-scroll

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

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

我在此组件中插入了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,我可以通过锚链接检测到更改,然后可以移至该特定部分。 (我有一些分页功能,可将我导航到“我的列表”和“团队列表”。)

页面加载后,我无法移至任一部分。 当页面加载/重新加载时,elementScrollToPart组件中作为null传递。

可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

这是答案,

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

希望这会有所帮助