1

In the below snippet, I am using extends to create components. which is not working. But If I use React.createClass({}); It works fine. Could you please let me know what the problem is in below code snippet

import React from 'react';

class App extends React.Component {
render() {
  return (
     <div>
        <AuditTable/>
     </div>
  );
 }
}

class AuditTable extends React.Component {
render(){
    return(
        <div> This is audit table 
            <AuditTable.Header/>
        </div>
    );
}
}

class AuditTable.Header extends React.Component{
render(){
    return(
        <div>
            Audit Table Header
        </div>
    );
}
}

export default App;

This code does not work. Any help would be great

1
  • 1
    class AuditTable.Header isn't legal. Rename it to Header or AuditTableHeader. Commented Jan 10, 2017 at 19:30

4 Answers 4

5
import React from 'react';

class App extends React.Component {
   render() {
      return (
         <div>
            <AuditTable/>
         </div>
      );
   }
}


var AuditTable = class AuditTable extends React.Component {
    render(){
        return(
            <div>
                <AuditTable.Header/>
                <AuditTable.Row/>
            </div>
        );
    }
}

AuditTable.Header = class Header extends React.Component{
    render(){
        return(
            <div>
                Audit Table Header
            </div>
        );
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

I got the solution to the question I posted

var AuditTable = class AuditTable extends React.Component { ... }
AuditTable.Header = class Header extends React.Component { ... }
AuditTable.Row = class Row extends React.Component { ... }

creating a class and assigning to ca variable worked

Thanks Guys

Comments

1

AuditTable.Header is not a valid class name (because it contains a dot). If you want to export your sub-components as static properties of your component, you have two possibilities :

1/ Declare a Header component, then assign it to AuditTable.Header

const Header extends React.Component{
  render(){
    return(
      <div>
        Audit Table Header
      </div>
    );
  }
}

AuditTable.Header = header;

2/ Declare directly Header as a static property of AuditTable

class AuditTable extends React.Component {

  static Header = class extends React.Component{
    render(){
      return(
        <div>
          Audit Table Header
        </div>
      );
    }
  }

  render(){
    return(
      <div> This is audit table 
        <AuditTable.Header/>
      </div>
    );
  }
}

Comments

0

Change AuditTable.Header to AuditTableHeader and your code should work as intended.

React.Component is not the name of the class you extend. There's an object named React that has a key named Component which is the class you extend.

1 Comment

how to do this with functional component?

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.