zoukankan      html  css  js  c++  java
  • [Recompose] Add Lifecycle Hooks to a Functional Stateless Component using Recompose

    Learn how to use the 'lifecycle' higher-order component to conveniently use hooks without using a class component.

    import React from 'react';
    import { withReducer, withHandlers, compose, lifecycle } from 'recompose';
    
    // Mock Configuration
    function fetchConfiguration() {
        return new Promise((resolve) => {
            setTimeout(() => resolve({
                                         showStatus: true,
                                         canDeleteUsers: true
                                     }), 300);
        });
    }
    const withConfig = lifecycle({
                                     getInitialState(){
                                         return { config: {} };
                                     },
                                     componentDidMount() {
                                         fetchConfiguration()
                                             .then((config) => {
                                                 this.setState({ config })
                                             })
                                     }
                                 })
    
    const UserStyle = {
        position: 'relative',
        background: 'lightblue',
        display: 'inline-block',
        padding: '10px',
        cursor: 'pointer',
        marginTop: '50px'
    };
    
    const StatusListStyle = {
        background: '#eee',
        padding: '5px',
        margin: '5px 0'
    };
    
    const TooltipStyle = {
        fontSize: '10px',
        position: 'absolute',
        top: '-10px',
         '80px',
        background: '#666',
        color: 'white',
        textAlign: 'center'
    };
    
    const StatusList = () =>
        <div style={StatusListStyle}>
            <div>pending</div>
            <div>inactive</div>
            <div>active</div>
        </div>;
    
    const withToggle = compose(
        withReducer('toggleState', 'dispatch', (state = false, action) => {
            switch( action.type ) {
                case 'SHOW':
                    return true;
                case 'HIDE':
                    return false;
                case 'TOGGLE':
                    return !state;
                default:
                    return state;
            }
        }, false),
        withHandlers({
                         toggle: ({ dispatch }) => (e) => dispatch({ type: 'TOGGLE' }),
                         show: ({ dispatch }) => (e) => dispatch({ type: 'SHOW' }),
                         hide: ({ dispatch }) => (e) => dispatch({ type: 'HIDE' })
                     })
    );
    
    const Statue = withToggle(
        ({ status, toggle, toggleState }) =>
            (<span onClick={() => toggle(!toggleState)}>
                {status}
                {toggleState && <StatusList/>}
            </span>)
    );
    
    const Tooltip = withToggle(({ show, hide, toggleState, text, children }) => (
        <span>
            <span>
                {toggleState && <div
                    style={TooltipStyle}>
                    { text }
                </div>}
                <span
                    onMouseOver={show}
                    onMouseOut={hide}
                >
                    { children }
                </span>
            </span>
        </span>
    ));
    
    const User3 = withConfig(({ status, name, config }) => (
        <div style={UserStyle}>
            <Tooltip text="Cool Dude!">{name}</Tooltip>-
            {config.showStatus && <Statue status={status}/>}
            {config.canDeleteUsers && <button>X</button>  }
        </div>
    ));
    
    export default User3;
  • 相关阅读:
    opencv视屏流嵌入wxpython框架
    Linux下makefile学习
    关于pyinstall打包时的依赖问题
    python文件结构与import用法
    python3+dlib人脸识别及情绪分析
    慕课学习--DNS的作用
    力扣leetcode11. 盛最多水的容器
    力扣leetcode5.最长回文子串
    力扣leetcode1190. 反转每对括号间的子串
    基于Ubuntu1604+ROS-kinetic+roscpp的激光雷达定位算法从零开始移植
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6854709.html
Copyright © 2011-2022 走看看