[React] Use React Context to Manage Application State Through Routes

來源:互聯網
上載者:User

標籤:ops   ble   sid   could   getch   cat   ring   history   this   

We’ll create a Router component that will wrap our application and manage all URL related state. We’ll see how we can use React’s built in context mechanism to pass data and functions between components without having to pass props all the way down through the component tree.

 

// index.jsReactDOM.render(    <MuiThemeProvider>        <Router><App /></Router></MuiThemeProvider>, document.getElementById(‘root‘));

On the top level, we add Router component to wrap our App component.

 

export class Router extends Component {    state = {        route: getCurrentPath()    };    handleLinkClick = (route) => {        this.setState({route});        history.pushState(null, ‘‘, route);    };    static childContextTypes = {        route: React.PropTypes.string,        linkHandler: React.PropTypes.func    };    getChildContext() {        return {            route: this.state.route,            linkHandler: this.handleLinkClick        };    }    render() {        return (          <div>{this.props.children}</div>        );    }}

We need to pass the props to the children so that Link component can know current url state. To do this, we using ‘Context‘ instead of ‘passing the props down to children‘.

Becasue there are two problems with this. One, in a complex app, that could potentially mean passing the same item down many levels. This could mean a lot of maintenance if things need to change.

The second problem is that, in this setup, app is being placed inside the router through a call to this.props.children. We can‘t just add props onto the app component in our render function. The way we‘re going to handle this is through React‘s context mechanism.

 

import React, {Component} from ‘react‘;const styles = {    padding: ‘8px‘};export class Link extends Component {    static contextTypes = {        route: React.PropTypes.string,        linkHandler: React.PropTypes.func    };    render() {        const activeClass = this.context.route === this.props.to ? ‘active‘: ‘‘;        const handleClick = (ev) => {            ev.preventDefault();            this.context.linkHandler(this.props.to);        };        return (            <a href="#" style={styles} className={activeClass} onClick={handleClick}>{this.props.children}</a>        );    }}Link.PropTypes = {    to: React.PropTypes.string.isRequired};

Last, in the Link component, we can use the Context to access what we have defined for Router compoent.

 

[React] Use React Context to Manage Application State Through Routes

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.