1

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.

4
  • 2
    you cannot use this.props in functional components, instead try props.children Commented Dec 10, 2018 at 5:31
  • props.children works. can you please explain why not use "this.props" Commented Dec 10, 2018 at 5:38
  • Only class components have props set to this.props, functional components as you see have an argument passed to them Commented Dec 10, 2018 at 5:41
  • even without constructor it works the state has no this how is it? Commented Dec 10, 2018 at 5:43

1 Answer 1

4

try this:

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

Don't use this.props in functional component.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.