How to create header and footer in React.js?

by lue.lemke , in category: JavaScript , 2 years ago

How to create header and footer in React.js?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by rashad_gerhold , 2 years ago

@lue.lemke You can create two separate components, header, and footer, and import them into your application in React.JS like in the example below:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import React from "react";
import ReactDOM from "react-dom";

const Header = () => {
  return (
    <header class="header">
      <p>Header Component</p>
    </header>
  );
};

const Footer = () => {
  return (
    <footer class="header">
      <p>Footer Component</p>
    </footer>
  );
};

const App = () => {
  return (
    <React.Fragment>
      <Header />
      <Footer />
    </React.Fragment>
  );
};

ReactDOM.render(<App />, document.getElementById("root"));
by kyla.kuvalis , a year ago

@lue.lemke 

You can create a header and footer in React by creating separate components for each and including them in your main component. Here is an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Header.js
import React from "react";

const Header = () => {
  return (
    <header>
      <nav>
        <h1>My header</h1>
      </nav>
    </header>
  );
};

export default Header;

// Footer.js
import React from "react";

const Footer = () => {
  return (
    <footer>
      <p>My footer</p>
    </footer>
  );
};

export default Footer;

// App.js
import React from "react";
import Header from "./Header";
import Footer from "./Footer";

const App = () => {
  return (
    <div>
      <Header />
      <main>
        <p>Main content</p>
      </main>
      <Footer />
    </div>
  );
};

export default App;


In this example, we create two separate components for the header and footer, and then include them in our main App component. The Header and Footer components can be reused in any other component by importing them.