我正在看redux-blog-example.有SignupRoute.js它看起来像这样:
@connect(state => ({
auth: state.auth
}), {
signup
})
export default class SignupRoute extends React.Component {
static contextTypes = {
router: React.PropTypes.object
}
handleSubmit = (email, password) => {
const router = this.context.router;
this.props.signup(email, password, router);
}
render() {
return (
<Signup
auth={this.props}
handleSubmit={this.handleSubmit}
/>
);
}
}
Run Code Online (Sandbox Code Playgroud)
如何router连接到context这个class?
Ste*_*ven 11
它使用context了一个未记录但反复广泛实现的React功能.对于完整的低位看看这篇文章,但这里是它的要点:
let router = Router(); // just illustrating, this is not how you instantiate React router
class MyComponent extends React.Component {
static contextTypes = {
router: React.PropTypes.object
};
render(){
// By declaring context type here, and childContextTypes
// on the parent along with a function with how to get it,
// React will traverse up and look for the `router` context.
// It will then inject it into `this.context`, making it
// available here.
}
}
class Parent extends React.Component {
static childContextTypes = {
router: React.PropTypes.object
};
getChildContext(){
return {
router: this.props.router
};
}
render(){
return <MyComponent />;
}
}
ReactDOM.render(<Parent router={router} />, document.getElementById('app'));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7109 次 |
| 最近记录: |