diff --git a/components/anchor/AnchorLink.tsx b/components/anchor/AnchorLink.tsx index ec2ea1335e..9e84899a81 100644 --- a/components/anchor/AnchorLink.tsx +++ b/components/anchor/AnchorLink.tsx @@ -1,5 +1,6 @@ import * as React from 'react'; import * as PropTypes from 'prop-types'; +import { polyfill } from 'react-lifecycles-compat'; import classNames from 'classnames'; import { AntAnchor } from './Anchor'; import { ConfigConsumer, ConfigConsumerProps } from '../config-provider'; @@ -12,7 +13,7 @@ export interface AnchorLinkProps { className?: string; } -export default class AnchorLink extends React.Component { +class AnchorLink extends React.Component { static defaultProps = { href: '#', }; @@ -29,10 +30,10 @@ export default class AnchorLink extends React.Component { this.context.antAnchor.registerLink(this.props.href); } - componentWillReceiveProps(nextProps: AnchorLinkProps) { - const { href } = nextProps; - if (this.props.href !== href) { - this.context.antAnchor.unregisterLink(this.props.href); + componentDidUpdate({ href: prevHref }: AnchorLinkProps) { + const { href } = this.props; + if (prevHref !== href) { + this.context.antAnchor.unregisterLink(prevHref); this.context.antAnchor.registerLink(href); } } @@ -79,3 +80,7 @@ export default class AnchorLink extends React.Component { return {this.renderAnchorLink}; } } + +polyfill(AnchorLink); + +export default AnchorLink;