user3802348
user3802348

Reputation: 2582

React component: cannot read property of undefined error

I am currently having a problem rendering a React wrapper component in another component. Basically, I export default the wrapper component (CreatePage), and then try to import it into another component (EditPage), but am getting the error "Cannot read property pageType of undefined." pageType is a prop for CreatePage that I define, but the weird thing is I have debuggers in the render functions of both Components and neither is being hit, so I think there's an issue with the component itself for some reason but I'm not sure quite what. Here is the code from the EditPage for some context:

import React, { Component, PropTypes } from 'react';
import CreatePage from './CreatePage';
import withResource from '../../../../lib/withResource';


export class EditPage extends Component {
    constructor(props){
        super(props);
    }

    render() {
        debugger;
        return (
            <div>
                <CreatePage pageType={'edit'}/>
            </div>
        );
    }
}

export default withResource(
{
    name: 'EditResource',
    key: 'hello',
    }, 
EditPage);

Code for CreatePage:

export default class CreatePage extends Component {

    constructor(props) {
        super(props);
    }

    headerTitles = {
        'create': i18n._('Create', '[m10n]'),
        'edit': i18n._('Edit', '[m10n]'),
    }

    renderHeader(pageType) {
        return (
            <div className={cx('headerWrapper')}>
                <div className={cx('header')}>
                    <div className={cx('title')}>
                        {this.headerTitles[pageType]}
                    </div>
                </div>
            </div>
        );
    }

    renderCreateEditPage(pageType) {
        return (
            <div>
                {this.renderHeader(pageType)}
                <div className={cx('contentWrapper')}>
                    <div>
                        <NameTag type={'pageType'}/>
                    </div>
                </div>
            </div>
        );
    }

    render() {
        return (
            <div className={cx('pageWrapper')}>
                {this.renderCreateEditPage(this.props.options.pageType)}
            </div>
        );
    }
}

Upvotes: 0

Views: 3044

Answers (1)

John
John

Reputation: 2894

CreatePage is looking in this.props.options.pageType but you just passed in pageType directly.. no mention of an options prop

Upvotes: 2

Related Questions