Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am a Gatsby newb. would like to make my Sidebar component more reusable by passing content from my home page (parent) component to the Sidebar UI (child) component.

I know I can use props to pass the data using the Sidebar component but I am struggling with implementing this approach with passing in and <ul> elements and would appreciate it if someone has been able to do that in the past and can show me how to read the data in the Sidebar.js file?

Is there a way to allow the Sidebar component to inherit the unorderedList property content from my home page component, please?

If it helps I got the code to my forked repo. You want the gatsby-integration-v2 branch which I should have already put inside this repo.

FYI when you compile the site, the sidebar component can be seen by the hamburger icon on the top right of the screen.

Below is a condensed version of my code if it helps.

index.js

JavaScript
import { Link } from "gatsby"
import React, { useState} from "react"
import { Sidebar } from "../components/sidebar"

const Home = () => {    
  return (
    <>
//Omitted code.
      {/* Wrapper */}
      <div id="wrapper" className="fade-in">     
          <Sidebar unorderedList={
            <ul>
              <Link to="#">Blog</Link>
            </ul>
            <ul>
              <Link to="#">home</Link>
            </ul>
            <h3>Social</h3>
            <ul className="icons alt">
              <li><a href="#" class="icon alt fa-twitter">Twitter</a></li>
              <li><a href="#" class="icon alt fa-facebook">Facebook</a></li>
              <li><a href="#" class="icon alt fa-instagram">Instagram</a></li>
              <li><a href="#" class="icon alt fa-github">GitHub</a></li>
            </ul>
          }</Sidebar>
        {/* Nav */}
          <nav id="nav">
            <ul className="links">
              <li className="active">
                <a href="index.html">This is Massively</a>
              </li>
            </ul>
            <ul className="icons">
              <li>
                <a href="#" className="icon fa-linkedin">
                  <span className="label">Instagram</span>
                </a>
              </li>
              <li>
                <a href="#" className="icon fa-github">
                  <span className="label">GitHub</span>
                </a>
              </li>
              <li><a href="#" class="icon alt fa-twitter">Twitter</a></li>
            </ul>
          </nav>
      </div>
    </>
  );
}

export default Home



sidebar.js
export const Sidebar =  () => {
  const [nav, showNav] = useState(false);
return(
<>
<Global />
          <MenuIcon nav={nav} onClick={() => showNav(!nav)}>
            <div/>
            <div/>
            <div/>
          </MenuIcon>
          <Menulinks nav={nav}>
            {/* <ul>
              <Link to="#">Blog</Link>
            </ul>
            <ul>
              <Link to="#">home</Link>
            </ul>
            <h3>Social</h3>
            <ul className="icons alt">
              <li><a href="#" class="icon alt fa-twitter">Twitter</a></li>
              <li><a href="#" class="icon alt fa-facebook">Facebook</a></li>
              <li><a href="#" class="icon alt fa-instagram">Instagram</a></li>
              <li><a href="#" class="icon alt fa-github">GitHub</a></li>
            </ul> */}
          </Menulinks>
    </>
  );
}


What I have tried:

I tried using this tutorial[^] but it didn’t help sadly. If only there was a tutorial that showed you how to pass list items from a parent to a child component.
Posted
Updated 3-Nov-22 3:27am
v2
Comments
Member 15627495 4-Nov-22 3:10am    
hello, whatever framework you use, all your elements are in the DOM of your page. you can copy them in another place, remove then, hide or show them by css. use a bit of native JS, or find in your framework which function does your need.
var _contents = document.getelementby...('the_parent_tag'); _target = document.getelementby...('the_target_tag'); _target.innerhtml(_contents);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900