anonomous
anonomous

Reputation: 27

TypeError: Cannot read property 'props' of undefined --reactjs

I'm having a tabcontainer component (child)uncontrolled component actually i dont know i'm doing it the right way and content component(parent)

contents.js

class Contents extends React.Component {
  state = {
    value: 0
  };

  handleChange = (event, value) => {
    this.setState({ value });
  };

  render() {
    const { classes } = this.props;
    const { value } = this.state;

    return (
      <div className={classes.root}>
        <AppBar position="static" color="default">
          <Tabs
            value={value}
            onChange={this.handleChange}
            scrollable
            scrollButtons="on"
            indicatorColor="primary"
            textColor="primary"
          >
            <Tab label="Wallet" />
            <Tab label="Transactions" />
            <Tab label="Add Fund" />
            <Tab label="Withdraw" />
            <Tab label="Add Bank Account" />
            <Tab label="Transfer" />
          </Tabs>
        </AppBar>
        {value === 0 && <TabContainer>Item One</TabContainer>}
        {value === 1 && <TabContainer>Item Two</TabContainer>}
        {value === 2 && <TabContainer>Item Three</TabContainer>}
        {value === 3 && <TabContainer>Item Four</TabContainer>}
        {value === 4 && <TabContainer>Item Five</TabContainer>}
        {value === 5 && <TabContainer>Item Six</TabContainer>}
      </div>
    );
  }
}

tabcontainer.js

import React from 'react';
import PropTypes from 'prop-types';
import Typography from '@material-ui/core/Typography';

const TabContainer = props => {
  return (
    <Typography component="div" style={{ padding: 8 * 3 }}>
      {this.props.children}
    </Typography>
  );
};

TabContainer.propTypes = {
  children: PropTypes.node.isRequired
};

export default TabContainer;

I get the following error TypeError: Cannot read property 'props' of undefined --reactjs.

I dont know whether I'm using props the right way passing in function prototype or I need class.

Can anyone lemme know where I'm going wrong.

Upvotes: 1

Views: 1643

Answers (1)

Anil Kumar
Anil Kumar

Reputation: 2309

try this:

   <Typography component="div" style={{ padding: 8 * 3 }}>
                {props.children}
   </Typography>

Don't use this.props in functional component.

Upvotes: 4

Related Questions