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.

  1. import React from 'react';
  2. import { lifecycle, branch, compose, renderComponent } from 'recompose';
  3.  
  4. const User = ({ name, status }) =>
  5. <div className="User">{ name }—{ status }</div>;
  6.  
  7. const withUserData = lifecycle({
  8. componentDidMount() {
  9. fetchData().then(
  10. (users) => this.setState({ users }),
  11. (error) => this.setState({ error })
  12. );
  13. }
  14. });
  15.  
  16. const UNAUTHENTICATED = ;
  17. const UNAUTHORIZED = ;
  18. const errorMsgs = {
  19. [UNAUTHENTICATED]: 'Not Authenticated!',
  20. [UNAUTHORIZED]: 'Not Authorized!',
  21. };
  22.  
  23. const AuthError = ({ error }) => error.statusCode && <div className="Error">{ errorMsgs[error.statusCode] }</div>;
  24.  
  25. const NoUsersMessage = () =>
  26. <div>There are no users to display</div>;
  27.  
  28. // Mock Service
  29. const noUsers = [];
  30. const users = [
  31. { name: "Tim", status: "active" },
  32. { name: "Bob", status: "active" },
  33. { name: "Joe", status: "inactive" },
  34. { name: "Jim", status: "pending" },
  35. ];
  36. function fetchData() {
  37. return new Promise((resolve, reject) => {
  38. setTimeout(() => {
  39. // reject({ statusCode: UNAUTHENTICATED });
  40. // reject({ statusCode: UNAUTHORIZED })
  41. // resolve(noUsers);
  42. resolve(users);
  43. }, );
  44. });
  45. }
  46.  
  47. const hasError = ({error}) => error && error.statusCode;
  48. const hasNoUser = ({users}) => users && users.length === ;
  49.  
  50. const enhance = compose(
  51. withUserData,
  52. branch(hasError, renderComponent(AuthError)),
  53. branch(hasNoUser, renderComponent(NoUsersMessage)),
  54. );
  55.  
  56. const User6 = enhance(({users}) => (
  57. <div className="UserList">
  58. { users && users.map((user) => <User {...user} />) }
  59. </div>
  60. ));
  61.  
  62. 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. 洛谷 P3505 [POI2010]TEL-Teleportation

    P3505 [POI2010]TEL-Teleportation 题目描述 King Byteasar is the ruler of the whole solar system that cont ...

  2. 用硬件卡克隆Linux集群

    650) this.width=650;" onclick="window.open("http://blog.51cto.com/viewpic.php?refimg= ...

  3. POJ 3009 Curling 2.0 {深度优先搜索}

    原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...

  4. phoenixframe自己主动化平台在Linux环境下运行用例的说明

    phoenixframe自己主动化平台支持在Linux环境下使用phantomjs,Firefox.chrome运行測试用例.但有下面几个问题须要注意: 1.若无法启动phantomjs,Firefo ...

  5. vue 键盘回车事件导致页面刷新的问题,路由多了一个问号

    问题: <el-form @submit.native.prevent> <el-form-item > <el-input @keyup.enter.native=&q ...

  6. GO语言学习(十三)Go 语言变量作用域

    Go 语言变量作用域 作用域为已声明标识符所表示的常量.类型.变量.函数或包在源代码中的作用范围. Go 语言中变量可以在三个地方声明: 函数内定义的变量称为局部变量 函数外定义的变量称为全局变量 函 ...

  7. 浅谈Normalize.css

    浅谈Normalize.css 一.总结 1.Normalize.css:它在默认的HTML元素样式上提供了跨浏览器的高度一致性,花了几百个小时来努力研究不同浏览器的默认样式的差异. 2.优于rese ...

  8. java中那些已经有的好用轮子

    后续补充 IOUtils http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/IOUtils.html ...

  9. python把一个列表画柱状图

    https://blog.csdn.net/w113691/article/details/80385534

  10. HDU 2063 过山车 第一道最大二分匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=2063 题目大意: m个女生和n个男生一起做过山车,每一排必须一男一女,而每个女孩愿意和一些男生坐一起,, 你要找 ...