【发布时间】:2016-04-16 14:11:06
【问题描述】:
我正在尝试使用 react-router(version ^1.0.3)做一个简单的重定向到另一个视图,我只是累了。
import React from 'react';
import {Router, Route, Link, RouteHandler} from 'react-router';
class HomeSection extends React.Component {
static contextTypes = {
router: PropTypes.func.isRequired
};
constructor(props, context) {
super(props, context);
}
handleClick = () => {
console.log('HERE!', this.contextTypes);
// this.context.location.transitionTo('login');
};
render() {
return (
<Grid>
<Row className="text-center">
<Col md={12} xs={12}>
<div className="input-group">
<span className="input-group-btn">
<button onClick={this.handleClick} type="button">
</button>
</span>
</div>
</Col>
</Row>
</Grid>
);
}
};
HomeSection.contextTypes = {
location() {
React.PropTypes.func.isRequired
}
}
export default HomeSection;
我只需要将使用发送到“/login”就可以了。
我能做什么?
控制台错误:
Uncaught ReferenceError: PropTypes is not defined
包含我的路线的文件
// LIBRARY
/*eslint-disable no-unused-vars*/
import React from 'react';
/*eslint-enable no-unused-vars*/
import {Route, IndexRoute} from 'react-router';
// COMPONENT
import Application from './components/App/App';
import Contact from './components/ContactSection/Contact';
import HomeSection from './components/HomeSection/HomeSection';
import NotFoundSection from './components/NotFoundSection/NotFoundSection';
import TodoSection from './components/TodoSection/TodoSection';
import LoginForm from './components/LoginForm/LoginForm';
import SignupForm from './components/SignupForm/SignupForm';
export default (
<Route component={Application} path='/'>
<IndexRoute component={HomeSection} />
<Route component={HomeSection} path='home' />
<Route component={TodoSection} path='todo' />
<Route component={Contact} path='contact' />
<Route component={LoginForm} path='login' />
<Route component={SignupForm} path='signup' />
<Route component={NotFoundSection} path='*' />
</Route>
);
【问题讨论】:
-
嗨!您能否发布您的
routes定义,以及是否有理由不使用Link组件?另外,请提及您遇到的错误。 -
而不是按钮,
<Link to="/login">Log In</Link>? -
另外你使用的是什么版本的 react-router?程序重定向的代码在主要版本之间发生了变化。
-
对于
Uncaught ReferenceError,你调用的是PropTypes,但你不导入它,你需要导入PropTypes本身或使用React.PropTypes -
@JoshDavidMiller 好点,但我不会感到惊讶
react-routerapi 在 5 分钟内更改.. 哈哈开玩笑,但只是部分
标签: javascript reactjs