I need the following component stucture:
JavaScript
x
7
1
import Layout from "./components/Layout";
2
<Layout>
3
<Layout.Header></Layout.Header>
4
<Layout.Body></Layout.Body>
5
<Layout.Footer></Layout.Footer>
6
</Layout>
7
I tried to achieve this as follows:
JavaScript
1
21
21
1
import React from "react";
2
3
const Layout = (props) => {
4
return <div className="layout">{props.children}</div>;
5
};
6
7
const Header = (props) => {
8
return <header className="layout__header">{props.children}</header>;
9
};
10
11
const Body = (props) => {
12
return <div className="layout__body">{props.children}</div>;
13
};
14
15
const Footer = (props) => {
16
return <footer className="layout__footer">{props.children}</footer>;
17
};
18
19
export { Header, Body, Footer };
20
export default Layout;
21
But this does not seem to work. Can anybody tell me how to export these functions right so that I can use the above structure?
Advertisement
Answer
Thanks to this answer Using dot notation with functional component I found the solution is:
JavaScript
1
22
22
1
const Layout = (props) => {
2
return <div className="layout layout--green">{props.children}</div>;
3
};
4
5
const Header = (props) => {
6
return <header className="layout__header">{props.children}</header>;
7
};
8
9
const Body = (props) => {
10
return <div className="layout__body">{props.children}</div>;
11
};
12
13
const Footer = (props) => {
14
return <footer className="layout__footer">{props.children}</footer>;
15
};
16
17
Layout.Header = Header;
18
Layout.Body = Body;
19
Layout.Footer = Footer;
20
21
export default Layout;
22