Let Me Tink About It
Let Me Tink About It

Reputation: 16132

Reactjs TypeError: Cannot read property 'style' of undefined

How do I interpret the following error?

TypeError: Cannot read property 'style' of undefined

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;

Upvotes: 3

Views: 6626

Answers (1)

Tom Coughlin
Tom Coughlin

Reputation: 463

I believe ...props.children.style is the source of your error. If you're doing something like...

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

then props.children will be undefined.

Upvotes: 4

Related Questions