[React] Keep Application State in Sync with Browser History

Using pushState and passing route data via context allows our application to respond to route changes made from Link components, but using the back and forward buttons in the browser doesn’t update the application state as we would expect. In this lesson, we’ll add event handling to our Router component to handle history popState events so routing behavior is maintained for the back and forward buttons.

import React, {Component} from ‘react‘;

const getCurrentPath = () => {
    const path = document.location.pathname;
    return path.substring(path.lastIndexOf(‘/‘));
};

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>
        );
    }

    componentDidMount() {
        window.onpopstate = () => {
            this.setState({route: getCurrentPath()})
        }
    }
时间: 2024-07-30 13:40:45

[React] Keep Application State in Sync with Browser History的相关文章

[转]Session and application state in ASP.NET Core

本文转自:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state By Rick Anderson and Steve Smith+ HTTP is a stateless protocol; the Web server treats each HTTP request as an independent request. The server retains no knowledge of variable va

React入门---属性(state)-7

state------>虚拟dom------>dom 这个过程是自动的,不需要触发其他事件来调用它. state中文理解:页面状态的的一个值,可以存储很多东西. 学习state的使用: 1.state先初始化,在React中,每一个类跟所有的面向对象的函数一样,都有一个构造函数叫constructor. 2.将state的初始化放在constructor()里面. export default class BodyIndex extends React.Component{ //将state

React组件的State

组件state必须能代表一个组件UI呈现的完整状态集,即组件的任何UI改变都可以从state的变化中反映出来:同时,state还必须代表一个组件UI呈现的最小状态集,即state中的所有状态都用于反映组件UI的变化,没有任何多余的状态,也不应该存在通过其他状态计算而来的中间状态. state vs 普通属性 首先,什么是普通属性? 我们的组件都是使用ES6的class定义的,所以组件的属性其实也就是class的属性(更确切的说法是class实例化对象的属性,但因为JavaScript本质上是没有

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

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 t

【05】react 之 组件state

1.1.  状态理解 React的数据流:由父节点传递到子节点(由外到内传递),如果顶层组件某个prop改变了,React会向下传递,重新渲染所有使用过该属性的组件.除此之外React 组件内部还具有自己的状态,这些状态只能在组件内部修改.通过与用户的交互(点击),实现不同状态(显示.隐藏.数量增加...),然后渲染UI,让用户界面和数据保持一致.React中只需更新组件的state,然后根据新的 state 重新渲染用户界面. this.props  属性:由父节点传入到组件内部,只读,不可修

React Native props &amp; state

今天又敲了一丁点代码,看了一下props和state的用法 原本以为state只是一个状态,但是又阅读了一下原文,才知道state是一组状态,这些状态是开发者自己定义的,都统一在state这个大类底下,跟props一样都是 this.props.propertyName this.state.stateName 这种形式,props和state是控制组件的两种类型,props是开发者自定义的组件参数,state表达的是一种状态用于控制组件的内容 /** * Sample React Native

[React] Configure a React &amp; Redux Application For Production Deployment and Deploy to Now

In this lesson, we’ll make a few small changes to our scripts and add some environment variables that will be used at build time to get our application ready to be deployed to a production environment using the now service. Once properly configured,

[Javascript] Manage Application State with Immutable.js

Learn how Immutable.js data structures are different from native iterable Javascript data types and why they provide an excellent foundation on which to build your application's state. Instead of them being mutable, they're always immutable, meaning

[React] Update Component State in React With Ramda Lenses

In this lesson, we'll refactor a React component to use Ramda lenses to update our component state. We'll create a lens to focus on the property we want to target and use over to apply the existing state value to a utility function and we'll get back