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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. RFID 读写器 Reader Writer Cloner

    RFID读写器的工作原理 RFID的数据采集以读写器为主导,RFID读写器是一种通过无线通信,实现对标签识别和内存数据的读出和写入操作的装置. 读写器又称为阅读器或读头(Reader).查询器(Int ...

  4. RFID Reader 线路图收集

    This 125 kHz RFID reader http://www.serasidis.gr/circuits/RFID_reader/125kHz_RFID_reader.htm http:// ...

  5. dfsdf

    This project was bootstrapped with Create React App. Below you will find some information on how to ...

  6. Foundations of Game Engine Development Volume 1 Mathematics (Eric Lengyel 著)

    http://www.foundationsofgameenginedev.com/ Chapter1 Vectors and Matrices (已看) Chapter2 Transforms (已 ...

  7. MEF and AppDomain z

    MEF and AppDomain - Remove Assemblies On The Fly This article will give an idea of what's involved i ...

  8. 使用create react app教程

    This project was bootstrapped with Create React App. Below you will find some information on how to ...

  9. React Native知识6-NavigatorIOS组件

    NavigatorIOS包装了UIKit的导航功能,可以使用左划功能来返回到上一界面.本组件并非由Facebook官方开发组维护.这一组件的开发完全由社区主导.如果纯js的方案能够满足你的需求的话,那 ...

随机推荐

  1. 洛谷 P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver

    P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 题目描述 The cows are out exercising their hooves again! There are N ...

  2. home.pl 正在促销,一些域名免费(终止于2017.4.4)

    home.pl 正在促销,一些域名免费(终止于2017.4.4) home.pl 成立于1997年,是波兰顶尖的互联网服务公司.专注于域名登记,托管网站,保持电子邮件帐户等.  home.pl 正在促 ...

  3. c#操作ElasticSearch5详解

    c#操作ElasticSearch详解 ElasticSearch系列学习 ElasticSearch第一步-环境配置 ElasticSearch第二步-CRUD之Sense ElasticSearc ...

  4. scroolspy滚动监听插件

    <nav id="nav" class="navbar navbar-default"> <a href="#" clas ...

  5. Java开源电商项目比較

    这里比較的都是国外的开源项目,备选项目有: Smilehouse Workspace.Pulse.Shopizer.ofbiz.bigfish.broadleaf 1.Smilehouse Works ...

  6. VIP的转移

    首先查看vip在各个节点的状态 [root@rac2 ~]# ifconfig eth0 Link encap:Ethernet HWaddr 08:00:27:C9:28:D0 inet addr: ...

  7. 计算机系统—CPU结构和内部工作

    一.计算机系统硬件组成 计算机系统的基本组成由:计算器.控制器.存储器.输入和输出设备这5大核心部件组成. 运算器和控制器等继承在一起成为CPU.以下通过这张图能够非常清楚的表达计算机系统.先从全局上 ...

  8. 关于JS面向对象、设计模式、以及继承的问题总结

    1.对象:JS中万物皆对象,它是一个泛指 类:对象的具体的细分 (物以类聚,人与群分.具有相同属性和方法的实例的一个集合总称) 实例:某一个类别中具体的一个事物 对象是一个抽象的概念,类似于我们的自然 ...

  9. FZU Problem 2168 防守阵地 I

    http://acm.fzu.edu.cn/problem.php?pid=2168 题目大意: 给定n个数和m,要求从n个数中选择连续的m个,使得a[i]*1+a[i+1]*2+--a[i+m]*m ...

  10. ntp 服务器

    ntp.sjtu.edu.cn 202.120.2.101 (上海交通大学网络中心NTP服务器地址)s1a.time.edu.cn 北京邮电大学s1b.time.edu.cn 清华大学s1c.time ...