Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I write Component inside Component in React?

Tags:

reactjs

I have come across many sources which talk about how to do Component nesting. However, whenever I try to create a Component inside another Component my code fails.

class parent extends React.Component{
    class child extends React.Component{
        render(){
            return <div><h1>Hiiii</h1></div>;
        }
    }
    render(){
        return <div><DEF /></div>;
    }
}  
like image 308
Kiran Avatar asked Nov 30 '22 14:11

Kiran


1 Answers

You can't do that. You can do this on the same file (not same component)

class DEF extends Component {
  render() {
   return (
    <div>
      <h1>Hiiii</h1>
    </div>
    );
 }
}

export default class ABC extends Component {
  render() {
       return (
       <div>
           <DEF />
       </div>
       );
   }
}
like image 92
Rodius Avatar answered Dec 04 '22 01:12

Rodius