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

时间:2018-11-03 20:38:12

标签: javascript reactjs runtime-error

如何解释以下错误?

  

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

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 个答案:

答案 0 :(得分:2)

我相信...props.children.style是您错误的根源。如果您正在执行类似的操作...

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

然后props.children将不确定。