代码之家  ›  专栏  ›  技术社区  ›  Let Me Tink About It

Reactjs TypeError:无法读取未定义的属性“style”

  •  2
  • Let Me Tink About It  · 技术社区  · 7 年前

    如何解释以下错误?

    TypeError:无法读取未定义的属性“style”

    import React from 'react';
    import PropTypes from 'prop-types';
    import {VelocityComponent} from 'velocity-react';
    import 'velocity-animate/velocity.ui';
    
    const FuseAnimate = (props) => {
        const children = React.cloneElement(props.children, {
            style: { // this line throws the error
                ...props.children.style,
                visibility: 'hidden'
            }
        });
        return (
            <VelocityComponent {...props} children={children}/>
        )
    
    };
    
    FuseAnimate.propTypes = {
        children: PropTypes.element.isRequired
    };
    
    FuseAnimate.defaultProps = {
        animation          : 'transition.fadeIn',
        runOnMount         : true,
        targetQuerySelector: null,
        interruptBehavior  : 'stop',
        visibility         : 'visible',
        duration           : 300,
        delay              : 50,
        easing             : [0.4, 0.0, 0.2, 1],
        display            : null
    };
    
    export default FuseAnimate;
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   Tom Coughlin    7 年前

    我相信 ...props.children.style 是你错误的根源。如果你在做像。。。

    render() {
      <FuseAnimate /> // no children
    }
    

    然后 props.children