2015-09-26 7 views
7

मैं 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} 
     /> 
    ); 
    } 
} 

कैसे करता router इस class की context अप करने के लिए तार हो सकते हैं?

उत्तर

10

यह context का उपयोग करता है, एक अनियंत्रित लेकिन काफी व्यापक रूप से कार्यान्वित प्रतिक्रिया सुविधा। पूर्ण डाउनडाउन के लिए this article देखें, लेकिन यहां इसकी जानकारी है:

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')); 
संबंधित मुद्दे