[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的方案能够满足你的需求的话,那 ...
随机推荐
- 洛谷 P3505 [POI2010]TEL-Teleportation
P3505 [POI2010]TEL-Teleportation 题目描述 King Byteasar is the ruler of the whole solar system that cont ...
- 用硬件卡克隆Linux集群
650) this.width=650;" onclick="window.open("http://blog.51cto.com/viewpic.php?refimg= ...
- POJ 3009 Curling 2.0 {深度优先搜索}
原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...
- phoenixframe自己主动化平台在Linux环境下运行用例的说明
phoenixframe自己主动化平台支持在Linux环境下使用phantomjs,Firefox.chrome运行測试用例.但有下面几个问题须要注意: 1.若无法启动phantomjs,Firefo ...
- vue 键盘回车事件导致页面刷新的问题,路由多了一个问号
问题: <el-form @submit.native.prevent> <el-form-item > <el-input @keyup.enter.native=&q ...
- GO语言学习(十三)Go 语言变量作用域
Go 语言变量作用域 作用域为已声明标识符所表示的常量.类型.变量.函数或包在源代码中的作用范围. Go 语言中变量可以在三个地方声明: 函数内定义的变量称为局部变量 函数外定义的变量称为全局变量 函 ...
- 浅谈Normalize.css
浅谈Normalize.css 一.总结 1.Normalize.css:它在默认的HTML元素样式上提供了跨浏览器的高度一致性,花了几百个小时来努力研究不同浏览器的默认样式的差异. 2.优于rese ...
- java中那些已经有的好用轮子
后续补充 IOUtils http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/IOUtils.html ...
- python把一个列表画柱状图
https://blog.csdn.net/w113691/article/details/80385534
- HDU 2063 过山车 第一道最大二分匹配
http://acm.hdu.edu.cn/showproblem.php?pid=2063 题目大意: m个女生和n个男生一起做过山车,每一排必须一男一女,而每个女孩愿意和一些男生坐一起,, 你要找 ...