[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的方案能够满足你的需求的话,那 ...
随机推荐
- cocos2dx——lua自己主动和手动绑定
[自己主动绑定] 參考:http://my.oschina.net/skyhacker2/blog/298397 主要是通过引擎自带的tools/tolua,主要过程例如以下: 1.编写好要导出的c+ ...
- [Python] List & Object spread in Python
def myfunc(x, y, z): print(x, y, z) tuple_vec = (, , ) dict_vec = {, , } >>> myfunc(*tuple_ ...
- DIV+CSS两种盒子模型(W3C盒子与IE盒子)
在辨析两种盒子模型之前.先简单说明一下什么叫盒子模型. 原理: 先说说我们在网页设计中常听的属性名:内容(content).填充(padding).边框(border).边界(margin), CSS ...
- Centos6.4安装opennebula
Centos6.4安装opennebula #安装163源 http://mirrors.163.com/.help/CentOS6-Base-163.repo #安装epel源 wget http: ...
- Windows 98 二十岁了,这些功能都是从它开始的(虽然 Windows 98 不如 Windows 95 那样具有革命性,但完成度更高,更加成熟。到最后还是:相见不如怀念。)
1998 年 6 月 25 日午夜,美国著名连锁零售店 CompUSA 门外挤满了狂热的消费者和媒体,他们在等待一款软件发售:Windows 98,即使明知它要到当天早上才正式上市. ▲ 在 Comp ...
- js05---js实现Map
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- actionBar-进入界面闪烁问题解决
问题分析: 主要是因为在开启一个应用的时候,当前界面并不是第一界面,在它之前,还有一个界面启动了,这个界面的唯一目的就是启动主界面,它目的不是显示.虽然如此,但是呢,这个界面的theme因为没有做统一 ...
- dom4j解析xml文件和字符串
转自:http://www.cnblogs.com/black-spike/p/9776180.html 最近在工作中,需要调别的接口,接口返回的是一个字符串,而且内容是xml格式的,结果在解析jso ...
- windows 静态和动态库
c++中共有两种库:1.动态链接库LIB包含了函数所在的DLL文件和文件中函数位置的信息(入口),代码由运行时加载在进程空间中的DLL提供,称为动态链接库dynamic link library.(这 ...
- BZOJ4044: [Cerc2014] Virus synthesis(回文树+DP)
Description Viruses are usually bad for your health. How about fighting them with... other viruses? ...