有人可以帮我弄清楚在 NavLink 组件中传递 Link 标签的意义是什么:
<NavLink tag={Link} to="/components/" activeClassName="active">Components</NavLink>
NavLink(reactstrap 组件)的代码如下:
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { mapToCssModules, tagPropType } from './utils';
const propTypes = {
tag: tagPropType,
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
disabled: PropTypes.bool,
active: PropTypes.bool,
className: PropTypes.string,
cssModule: PropTypes.object,
onClick: PropTypes.func,
href: PropTypes.any,
};
const defaultProps = {
tag: 'a',
};
class NavLink extends React.Component {
constructor(props) {
super(props);
this.onClick = this.onClick.bind(this);
}
onClick(e) {
if (this.props.disabled) {
e.preventDefault();
return;
}
if (this.props.href === '#') {
e.preventDefault();
}
if (this.props.onClick) {
this.props.onClick(e);
}
}
render() {
let {
className,
cssModule,
active,
tag: Tag,
innerRef,
...attributes
} = this.props;
const classes = mapToCssModules(classNames(
className,
'nav-link',
{
disabled: attributes.disabled,
active: active
}
), cssModule);
return (
<Tag {...attributes} ref={innerRef} onClick={this.onClick} className={classes} />
);
}
}
NavLink.propTypes = propTypes;
NavLink.defaultProps = defaultProps;
export default NavLink;
在这里您可以看到 NavLink 返回一个包装在我们作为 props 传递的标签内的组件。 Link(react-router组件)的基本功能,即路由组件,这里没有完成。因此将它作为 NavLink 的支柱传递让我感到困惑。