In this lesson we'll create a protected route just for logged in users. We'll combine a Route with a render prop and use a loggedIn prop to determine if the route should be allowed to be accessed. Finally we'll use nav state to preserve the location the user visited and redirect them back to the protected route once they login.

1. Create a ProtectedRoute is nothing but just a react component render a Route component:

  • check the 'loggedIn' props, if true, then using render prop to render the component normally.
  • If 'loggedIn' props is false, then use 'Redirect' component to redirect to Home page. also pass the route state.
const ProtectedRoute = ({ component: Comp, loggedIn, path, ...rest }) => {
return (
<Route
path={path}
{...rest}
render={(props) => {
return loggedIn ? (
<Comp {...props} />
) : (
<Redirect
to={{
pathname: "/",
state: {
prevLocation: path,
error: "You need to login first!",
},
}}
/>
);
}}
/>
);
};

2. When the login button is clicked, we want to force udpate the state using callback in setState:

    this.setState(
{
loggedIn: true,
},
() => {
this.props.history.push(prevLocation || "/");
},
);
};

setState() does not always immediately update the component. It may batch or defer the update until later. This makes reading this.state right after calling setState() a potential pitfall. Instead, use componentDidUpdate or a setState callback (setState(updater, callback)), either of which are guaranteed to fire after the update has been applied. If you need to set the state based on the previous state, read about the updater argument below. Link

[React Router] Create a ProtectedRoute Component in React Router (setState callback to force update)的更多相关文章

  1. [React Native] Use the SafeAreaView Component in React Native for iPhone X Compatibility

    In this lesson, you will learn how to use the SafeAreaView component to avoid the sensor cluster (th ...

  2. [React Native] Create a component using ScrollView

    To show a list of unchanging data in React Native you can use the scroll view component. In this les ...

  3. [React Native] Using the Image component and reusable styles

    Let's take a look at the basics of using React Native's Image component, as well as adding some reus ...

  4. [React] Refactor a Class Component with React hooks to a Function

    We have a render prop based class component that allows us to make a GraphQL request with a given qu ...

  5. React.Component 与 React.PureComponent(React之性能优化)

    前言 先说说 shouldComponentUpdate 提起React.PureComponent,我们还要从一个生命周期函数 shouldComponentUpdate 说起,从函数名字我们就能看 ...

  6. [React] Use React.memo with a Function Component to get PureComponent Behavior

    A new Higher Order Component (HOC) was recently released in React v16.6.0 called React.memo. This be ...

  7. React.Component 和 React.PureComponent 、React.memo 的区别

    一 结论 React.Component 是没有做任何渲染优化的,但凡调用this.setState 就会执行render的刷新操作. React.PureComponent 是继承自Componen ...

  8. how to design a search component in react

    how to design a search component in react react 如何使用 React 设计并实现一个搜索组件 实时刷新 节流防抖 扩展性,封装,高内聚,低耦合 响应式 ...

  9. React 源码剖析系列 - 不可思议的 react diff

      简单点的重复利用已有的dom和其他REACT性能快的原理. key的作用和虚拟节点 目前,前端领域中 React 势头正盛,使用者众多却少有能够深入剖析内部实现机制和原理. 本系列文章希望通过剖析 ...

随机推荐

  1. badboy提示脚本错误解决方法

    1.输入URL,提示脚本错误 解决办法:打开IE浏览器,工具->internet选项->高级,如图所示去掉禁用脚本调试 2.badboy内置浏览器,提示脚本错误解决办法 解决办法:badb ...

  2. 使用getopt命令解析shell脚本的命令行选项 【转】

    本文转载自:http://yejinxin.github.io/parse-shell-options-with-getopt-command 在之前的一篇文章中,介绍了如何利用shell内置的get ...

  3. Triangle LOVE(拓扑排序)

    Triangle LOVE Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) Total ...

  4. Java数组和内存控制

    1.数组初始化 1.1 Java数组是静态的 Java语言是典型的静态语言,因此Java的数组是静态的,即当数组被初始化之后,该数组的长度是不可变的.Java程序中的数组必须经初始化才可使用.所谓初始 ...

  5. wamp服务器下安装mantis

    什么是Mantis Mantis是一个BUG管理系统.主要特点如下: 1.用php写的系统,安装方便,不用像 bugzilla 那样安装那么多perl支持: 2.系统相对简单轻磅,使用简单: 3.出色 ...

  6. C# 数组动态添加新元素的 方法

    经常在开发中  会对字符串 进行split 拆分操作.. 得到数组后再去做相应的事情! 但有时候,需求决定了 数组的长度 不是固定的, 而C# 数组 是不允许动态添加新的元素的.. 这事情让我也纠结了 ...

  7. 前后端分离开发,基于SpringMVC符合Restful API风格Maven项目实战(附完整Demo)!

    摘要: 本人在前辈<从MVC到前后端分离(REST-个人也认为是目前比较流行和比较好的方式)>一文的基础上,实现了一个基于Spring的符合REST风格的完整Demo,具有MVC分层结构并 ...

  8. WRAR下载及注册

    下载过程: 1.打开winrar官网:https://www.win-rar.com 2.点击下载winrar按钮,如上图所示 3.进入下一页面,点击下载按钮即可完成下载过程 注册过程:https:/ ...

  9. vue-cli简介(中文翻译)

    vue-cli是一个简单的vuejs脚手架命令行工具. 安装 准备:Node.js(>=4.x,推荐6.x版本),npm版本3以上和Git. $npm install -g vue-cli 使用 ...

  10. [C]关于交换

    交换(c,c++): 1)temp交换(也适用于非数型) 定义一个新的变量,借助它完成交换. int a,b; a=10; b=15; int t; t=a; a=b; b=t; 2)位运算 位运算不 ...