Learn how to use the 'branch' and 'renderComponent' higher-order components to show a spinner while a component loads.

  1. import React from 'react';
  2. import { lifecycle, branch, compose, renderComponent } from 'recompose';
  3.  
  4. // Mock Configuration
  5. function getUser() {
  6. return new Promise((resolve, reject) => {
  7. setTimeout(() => resolve({
  8. name: 'Zhentian',
  9. status: 'active'
  10. }), );
  11. })
  12. }
  13.  
  14. const Spinner = () =>
  15. <div className="Spinner">
  16. <div className="loader">Loading...</div>
  17. </div>;
  18.  
  19. const withUserData = lifecycle({
  20. getInitialState(){
  21. return {
  22. loading: true
  23. }
  24. },
  25. componentDidMount() {
  26. getUser()
  27. .then((user) => {
  28. this.setState({...user, loading: false})
  29. })
  30. }
  31. });
  32.  
  33. const UserStyle = {
  34. position: 'relative',
  35. background: 'lightblue',
  36. display: 'inline-block',
  37. padding: '10px',
  38. cursor: 'pointer',
  39. marginTop: '50px'
  40. };
  41.  
  42. const withSpinnerWhileLoading = branch(
  43. (props) => props.loading,
  44. renderComponent(Spinner)
  45. );
  46.  
  47. const enhance = compose(
  48. withUserData,
  49. withSpinnerWhileLoading
  50. )
  51.  
  52. let User5 = enhance(({ name, status }) => (
  53. <div style={UserStyle}>
  54. {name} - {status}
  55. </div>
  56. ));
  57.  
  58. export default User5;

[Recompose] Show a Spinner While a Component is Loading using Recompose的更多相关文章

  1. [Recompose] Lock Props using Recompose -- withProps

    Learn how to use the ‘withProps’ higher order component to pre-fill a prop, unable to be overridden. ...

  2. [React] Recompose: Theme React Components Live with Context

    SASS Bootstrap allows us to configure theme or branding variables that affect all components (e.g. P ...

  3. 如何优雅的使用vue+vux开发app -03

    如何优雅的使用vue+vux开发app -03 还是一个错误的示范,但是离优雅差的不远了... <!DOCTYPE html> <html> <head> < ...

  4. 如何优雅的使用vue+vux开发app -02

    如何优雅的使用vue+vux开发app -02 很明显这又是一个错误的示范,请勿模仿 使用动态组件实现保留状态的路由 <!DOCTYPE html> <html> <he ...

  5. 如何优雅的使用vue+vux开发app -01

    如何优雅的使用vue+vux开发app -01 很明显下面是个错误的示范: <!DOCTYPE html> <html> <head> <title>v ...

  6. 当初要是看了这篇,React高阶组件早会了

    当初要是看了这篇,React高阶组件早会了. 概况: 什么是高阶组件? 高阶部件是一种用于复用组件逻辑的高级技术,它并不是 React API的一部分,而是从React 演化而来的一种模式. 具体地说 ...

  7. Angular 2 to Angular 4 with Angular Material UI Components

    Download Source - 955.2 KB Content Part 1: Angular2 Setup in Visual Studio 2017, Basic CRUD applicat ...

  8. React项目 - 几种CSS实践

    前言团队在使用react时,不断探索,使用了很多不同的css实现方式,此篇blog总结了,react项目中常见的几种css解决方案:inline-style/radium/style-componen ...

  9. Suspense for Data Fetching

    Suspense for Data Fetching Experimental https://reactjs.org/docs/concurrent-mode-suspense.html React ...

随机推荐

  1. 让JavaScript在Visual Studio 2015中编辑得更easy

    微软公布的Visual Studio 2015展示了该公司对于让该开发工具更好的支持主流的开发语言的工作.微软项目经理Jordan Matthiesen已经具体列出了一些具体处理JavaScript开 ...

  2. HDU 2102 A计划 (三维的迷宫BFS)

    题目链接:pid=2102">传送门 题意: 三维的一个迷宫,起点在第一层的S(0,0,0)处,问是否能在规定的时间内走到第二层的P 处.'*'代表不能走,'.'代表能够走,'#'代表 ...

  3. android 弹幕评论效果

    纯粹依照自己的想法仿照b站的弹幕写的一个demo,不知道正确的姿势怎么样的. demo下载地址 首先.一条弹幕就是一个textview public abstract class Danmu exte ...

  4. worktools-git 工具的使用总结(知识点累积)

    1.用简单列表的方式查看提交记录git log --pretty=online zhangshuli@zhangshuli-MS-:~/myGit$ git log --pretty=oneline ...

  5. Angularjs:实现全选

    html: <div class="input-group"> <span class="input-group-addon" style=& ...

  6. Linux登录状态

    1.hostname 显示主机名称.设置主机名称 1)显示主机名称 xiaohuang@xiaohuang-virtual-machine:~$ hostname xiaohuang-virtual- ...

  7. 解决ListCtrl控件第一列文字不能居中显示的问题/修改网格线

    把CListCtrl设置为Report风格,但是插入第一列的时候(InsertColumn)的时候会发现文字不能居中.即使使用了LVCFMT_CENTER,其他列都可以正常居中,但第一列仍然靠左显示. ...

  8. APM2.8 Rover 自己主动巡航车设计(固件安装和设置)

    1.2 APM2.8软件安装与固件下载 下载Mission Planner这个地面基站软件,这里介绍的是windoews平台下的,在MAC或者linux下能够使用QGroundCont基于QT编写的地 ...

  9. MDaemon and Apache2

    MDaemon and Apache2 邮件服务器列表 http://down.chinaz.com/class/93_1.htm MDaemon11.03中文破解补丁 http://download ...

  10. Bitmap-把方形图片处理为圆形

    这个是直接在网上转载的,自己验证可靠 转载自http://my.oschina.net/zhouz/blog/213164 直接贴上代码 import android.graphics.Bitmap; ...