[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的方案能够满足你的需求的话,那 ...
随机推荐
- IOS - 获取UITextField的输入文本
当UITextField文本改变时, 依据内容更新数据, 通过写监听事件就可以. 加入监听: [timesField addTarget:self action:@selector(textField ...
- RK3066 实现LED闪烁的代码分析
实现LED灯的闪烁,须要在驱动里加入一个定时器函数,详细实现涉及到了LED GPIO驱动.用户空间程序调用驱动程序. 1.首先来看LED设备驱动注冊过程,代码位于../kernel/drivers/l ...
- 正确理解Widget::Widget(QWidget *parent) :QWidget(parent)这句话(初始化列表中无法直接初始化基类的数据成员,所以你需要在列表中指定基类的构造函数)
最近有点忙,先发一篇我公众号的文章,以下是原文. /********原文********/ 最近很多学习Qt的小伙伴在我的微信公众号私信我,该如何理解下面段代码的第二行QWidget(parent) ...
- git- 仓库创建、修改、提交、撤销
1.仓库创建 zhangshuli@zhangshuli-MS-:~$ mkdir myGit zhangshuli@zhangshuli-MS-:~$ cd myGit/ zhangshuli@zh ...
- BZOJ2733: [HNOI2012]永无乡(线段树合并)
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
- [Node.js] Serve Static Files with Express
In this lesson we will find out how to serve static assets (images, css, stylesheets, etc.) with Exp ...
- [D3] Drawing path in D3
Here we have a force layout with three nodes. In the example, we will link three nodes with line and ...
- 【软件project】 文档 - 银行业务管理 - 需求分析
---------------------------------------------------------------------------------------------------- ...
- leetCode 103.Binary Tree Zigzag Level Order Traversal (二叉树Z字形水平序) 解题思路和方法
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- 芯片AMS1117 电源芯片