我對ReactJS非常陌生,而且我真的被這個問題困住了,所以請耐心等待。我想知道是否有方法使用反應路由器將活動類添加到我的導航鏈接。這是我的代碼看起來像....如何將活動類添加到我的導航鏈接React路由器?
import React, { Component, PropTypes } from 'react';
import history from '../../core/history';
function isLeftClickEvent(event) {
return event.button === 0;
}
function isModifiedEvent(event) {
return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
}
class Link extends Component { // eslint-disable-line react/prefer-stateless- function
static propTypes = {
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
onClick: PropTypes.func
};
handleClick = (event) => {
let allowTransition = true;
if (this.props.onClick) {
this.props.onClick(event);
}
if (isModifiedEvent(event) || !isLeftClickEvent(event)) {
return;
}
if (event.defaultPrevented === true) {
allowTransition = false;
}
event.preventDefault();
if (allowTransition) {
if (this.props.to) {
history.push(this.props.to);
} else {
history.push({
pathname: event.currentTarget.pathname,
search: event.currentTarget.search
});
}
}
};
render() {
const { to, ...props } = this.props; // eslint-disable-line no-use-before-define
return <a href={history.createHref(to)} {...props} onClick={this.handleClick} />;
}
}
export default Link;
import React, { PropTypes } from 'react';
import cx from 'classnames';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import s from './Navigation.css';
import Link from '../Link';
function Navigation({ className }) {
return (
<div className={cx(s.root, className)} role="navigation">
<Link className={s.link + ' fa fa-dribbble'} to="/dribbble"/>
<Link className={s.link + ' fa fa-behance' } to="/behance"/>
<Link className={s.link + ' fa fa-linkedin' } to="/linkedin"/>
<Link className={s.link + ' fa fa-twitter' } to="/twitter"/>
<Link className={s.link + ' fa fa-instagram' } to="/instagram"/>
<Link className={s.link + ' fa fa-vimeo' } to="/vimeo"/>
</div>
);
}
Navigation.propTypes = {
className: PropTypes.string
};
export default withStyles(s)(Navigation);
我想要做的就是添加活動類像這樣的例子:
<a class="link active">Home</a>
<a class="link">About</a>
<a class="link">Contact</a>
<a class="link">Work</a>
<a class="link">Support</a>
如何做到這一點任何想法?我一直在瞎搞與反應,但我不能找到一個簡單的方法來做到這一點..
是的,我認爲多數民衆贊成以正確的方式去了解它,但事實是我沒有使用這個樣板反應路由器...它帶有一個自定義鏈接組件。這裏是我使用https://github.com/kriasoft/react-starter-kit的Boilerplate –