[Recompose] Replace a Component with Non-Optimal States using Recompose
Learn how to use the ‘branch’ and ‘renderComponent’ higher-order components to show errors or messaging when your component is in a non-optimal state. Avoid putting extraneous logic to show errors or messaging into your core component by organizing your non-optimal states into custom higher-order components.
import React from 'react';
import { lifecycle, branch, compose, renderComponent } from 'recompose'; const User = ({ name, status }) =>
<div className="User">{ name }—{ status }</div>; const withUserData = lifecycle({
componentDidMount() {
fetchData().then(
(users) => this.setState({ users }),
(error) => this.setState({ error })
);
}
}); const UNAUTHENTICATED = ;
const UNAUTHORIZED = ;
const errorMsgs = {
[UNAUTHENTICATED]: 'Not Authenticated!',
[UNAUTHORIZED]: 'Not Authorized!',
}; const AuthError = ({ error }) => error.statusCode && <div className="Error">{ errorMsgs[error.statusCode] }</div>; const NoUsersMessage = () =>
<div>There are no users to display</div>; // Mock Service
const noUsers = [];
const users = [
{ name: "Tim", status: "active" },
{ name: "Bob", status: "active" },
{ name: "Joe", status: "inactive" },
{ name: "Jim", status: "pending" },
];
function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
// reject({ statusCode: UNAUTHENTICATED });
// reject({ statusCode: UNAUTHORIZED })
// resolve(noUsers);
resolve(users);
}, );
});
} const hasError = ({error}) => error && error.statusCode;
const hasNoUser = ({users}) => users && users.length === ; const enhance = compose(
withUserData,
branch(hasError, renderComponent(AuthError)),
branch(hasNoUser, renderComponent(NoUsersMessage)),
); const User6 = enhance(({users}) => (
<div className="UserList">
{ users && users.map((user) => <User {...user} />) }
</div>
)); export default User6;
[Recompose] Replace a Component with Non-Optimal States using Recompose的更多相关文章
- [Recompose] Handle React Events as Streams with RxJS and Recompose
		
Events are the beginning of most every stream. Recompose provides a createEventHandler function to h ...
 - [Recompose] Set the HTML Tag of a Component via a Prop using Recompose
		
Learn how to user the ‘componentFromProp’ helper and ‘defaultProps’ higher order component to swap t ...
 - RFID 读写器 Reader Writer Cloner
		
RFID读写器的工作原理 RFID的数据采集以读写器为主导,RFID读写器是一种通过无线通信,实现对标签识别和内存数据的读出和写入操作的装置. 读写器又称为阅读器或读头(Reader).查询器(Int ...
 - RFID Reader 线路图收集
		
This 125 kHz RFID reader http://www.serasidis.gr/circuits/RFID_reader/125kHz_RFID_reader.htm http:// ...
 - dfsdf
		
This project was bootstrapped with Create React App. Below you will find some information on how to ...
 - Foundations of Game Engine Development Volume 1 Mathematics (Eric Lengyel 著)
		
http://www.foundationsofgameenginedev.com/ Chapter1 Vectors and Matrices (已看) Chapter2 Transforms (已 ...
 - MEF and AppDomain  z
		
MEF and AppDomain - Remove Assemblies On The Fly This article will give an idea of what's involved i ...
 - 使用create react app教程
		
This project was bootstrapped with Create React App. Below you will find some information on how to ...
 - React Native知识6-NavigatorIOS组件
		
NavigatorIOS包装了UIKit的导航功能,可以使用左划功能来返回到上一界面.本组件并非由Facebook官方开发组维护.这一组件的开发完全由社区主导.如果纯js的方案能够满足你的需求的话,那 ...
 
随机推荐
- sqlserver存储过程实现多表分页
			
if @PageIndex = 1 begin if @strWhere != ’’ set @strSQL = ’select top ’ + str(@PageSize) +’ ’+@strGet ...
 - C++编码优化之减少冗余拷贝或赋值
			
临时变量 目前遇到的一些产生临时变量的情况:函数实参.函数返回值.隐式类型转换.多余的拷贝 1. 函数实参 这点应该比较容易理解,函数参数,如果是实参传递的话,函数体里的修改并不会影响调用时传入的参数 ...
 - 记2018/4/29 qbxt 测试
			
记 2018/4/29 qbxt 测试(提高基础班) 简单的 NOIP 模拟赛 竞赛时间: 2018 年 4 月 29 日 13:30-17:00 题目名称 乘法 求和 计数 输入文件名 mul.i ...
 - cwRsync 同步时报错 STATUS_ACCESS_VIOLATION
			
cwRsync 同步时报错 STATUS_ACCESS_VIOLATION windows XP 执行 cwRsync 同步时报错: 2 [main] rsync 3044 _cygtls::h ...
 - ajax 通过return 返回data值
			
方法例如以下: 1. ajax 必须为同步 设置async:false 2. 定一个局部变量 把data赋值给局部变量 然后 return 局部变量就可以 示比例如以下 function getEmp ...
 - !!在JS中代表什么
			
!!一般用来将后面的表达式转换为布尔型的数据(boolean), javascript约定和c类似,规则为 ·false.undefinded.null.0."" 为 false, ...
 - 关于pcb铺铜
 - JS学习笔记 - 面向对象 - 原型
			
<script> var arr1 = new Array(12, 55, 34, 78, 676); var arr2 = new Array(12, 33, 1) Array.prot ...
 - Eclipse手动配置svn
			
1.在Eclipse根目录下建一个任意文件夹(如plugin),在该文件夹下建一个以该插件名命名的文件夹(如SVN).2.将下载下的插件文件解压,plugins和features文件夹复制到该文件夹下 ...
 - AspJpeg2.0组件教程完整版 aspjpeg教程...
			
AspJpeg是一款功能强大的基于Microsoft IIS环境的图片处理组件,网络上对其进行详细和深入介绍的中文文章并不多,即使有一般也只是牵涉到图片缩略图和图片水印,这与其为英文版本有着密切的关系 ...