[React Router] Create a ProtectedRoute Component in React Router (setState callback to force update)
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 readingthis.stateright after callingsetState()a potential pitfall. Instead, usecomponentDidUpdateor asetStatecallback (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 theupdaterargument below. Link
[React Router] Create a ProtectedRoute Component in React Router (setState callback to force update)的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- React.Component 与 React.PureComponent(React之性能优化)
前言 先说说 shouldComponentUpdate 提起React.PureComponent,我们还要从一个生命周期函数 shouldComponentUpdate 说起,从函数名字我们就能看 ...
- [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 ...
- React.Component 和 React.PureComponent 、React.memo 的区别
一 结论 React.Component 是没有做任何渲染优化的,但凡调用this.setState 就会执行render的刷新操作. React.PureComponent 是继承自Componen ...
- how to design a search component in react
how to design a search component in react react 如何使用 React 设计并实现一个搜索组件 实时刷新 节流防抖 扩展性,封装,高内聚,低耦合 响应式 ...
- React 源码剖析系列 - 不可思议的 react diff
简单点的重复利用已有的dom和其他REACT性能快的原理. key的作用和虚拟节点 目前,前端领域中 React 势头正盛,使用者众多却少有能够深入剖析内部实现机制和原理. 本系列文章希望通过剖析 ...
随机推荐
- [POJ 3345] Bribing FIPA
[题目链接] http://poj.org/problem?id=3345 [算法] 树形背包 [代码] #include <algorithm> #include <bitset& ...
- grpc vs2015编译
获取gRPC源码 gRPC是开源框架,项目代码在github上,所以首先要安装github.github安装后,在指定文件夹中,执行git命令就可以获取gRPC的所有源码. git clone ht ...
- Python多线程学习(一、线程的使用)
Python中使用线程有两种方式:函数或者用类来包装线程对象. 1. 函数式:调用thread模块中的start_new_thread()函数来产生新线程.如下例: import thread de ...
- html5之文件操作
用来把文件读入内存,并且读取文件中的数据.FileReader接口提供了一个异步API,使用该API可以在浏览器主线程中异步访问文件系统,读取文件中的数据.到目前文职,只有FF3.6+和Chrome6 ...
- Solr.NET快速入门(七)【覆盖默认映射器,NHibernate集成】
覆盖默认映射器 默认情况下,SolrNet使用属性映射Solr字段. 但是,您可能需要使用另一个映射程序. 替换默认映射器取决于您如何设置库: 内置容器 如果使用默认的内置容器,可以在调用Startu ...
- 实现model中的文件上传FTP(二)
上一篇博客记录了如何将model中的图片存入FTP,通过一个第三方的storages简单的实现了,但是后续我发现如果想在浏览器通过url直接获取图片,就不太容易了(大神轻喷,小弟自学django和py ...
- P1284 三角形牧场
题目描述 和所有人一样,奶牛喜欢变化.它们正在设想新造型的牧场.奶牛建筑师Hei想建造围有漂亮白色栅栏的三角形牧场.她拥有N(3≤N≤40)块木板,每块的长度Li(1≤Li≤40)都是整数,她想用所有 ...
- express jade ejs 为什么要用这些?
express是快速构建web应用的一个框架 线上文档 http://www.expressjs.com.cn/ 不用express行不行呢? 看了网上的回答:不用express直接搭,等你 ...
- 在MyEclipse中使用debug模式
转:http://blog.csdn.net/competerh_programing/article/details/6773371 1, 首先在一个java文件中设断点,然后运行,当程序走到断点 ...
- 远程连接windows出现身份验证错误,提示"由于CredSSP加密Oracle修正"解决方案
本机操作系统(OS版本:10.0.17134) 远程计算机操作系统(OS版本:6.3.9600) 远程连接的时候报错“出现身份验证错误,要求的函数不受支持.远程计算机:xxx 这可能是由于CredSS ...