reactjs 无法使用带有Hydrogen / React的.server.jsx组件创建响应导航栏

pbossiut  于 2023-01-04  发布在  React
关注(0)|答案(1)|浏览(99)

我不能使我的导航栏响应,我不知道为什么。当我在Layout. server. jsx中设置一个常量时,会出错。
我想我不能在按钮上创建onClick事件,但我不知道为什么以及如何解决它。
You can find the git repository here: https://github.com/imnotremi/hydrogen-test
请注意,在hydrogen.config.js中,您必须输入氢气的免费使用凭证:
Shopify Domain : "hydrogen-preview.myshopify.com" Storfront Acces Token : "3b580e70970c4528da70c98e097c2fa0"
下面是我的代码:

import { useShopQuery, CacheLong, gql, Seo, Link } from "@shopify/hydrogen"; 
import { Suspense } from "react";
import CartBubble from "./CartBubble.client";

export default function Layout({children}) { 

    const data = useShopQuery({ 
    query: SHOP_QUERY, 
    cache: CacheLong(), 
    preload: true,
    }); 

    const { data: { shop }, } = data; 

    return ( 

    <>

        <Seo 
            type="defaultSeo"
            data={{
                title: shop.name,
                description: shop.description
            }}
        />

        <header className="">

            <div className="text-center bg-transparent">

                <div className=" container flex items-center justify-between py-4 ">


                    <Link to="/" className=" font-bold logo min-w-[33.33%]">
                    {shop.name}
                    </Link>


                    <ul className=" flex place-self-center text-center justify-around list-none font-nav gap-3 max-w-[33.33%]">
                        <li className="text-slate-600 hover:text-slate-800"><a href="/catalog">Catalog</a></li>
                        <li className="text-slate-600 hover:text-slate-800"><a href="/collections/freestyle">Freestyle</a></li>
                        <li className="text-slate-600 hover:text-slate-800"><a href="/Aboutus">About us</a></li>
                    </ul>


                    <Link to="/cart" className="flex items-center justify-end font-nav gap-3 w-[33.33%] text-slate-600 hover:text-slate-800">
                        <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="w-6 h-6">
                            <path strokeLinecap="round" strokeLinejoin="round" d="M2.25 3h1.386c.51 0 .955.343 1.087.835l.383 1.437M7.5 14.25a3 3 0 00-3 3h15.75m-12.75-3h11.218c1.121-2.3 2.1-4.684 2.924-7.138a60.114 60.114 0 00-16.536-1.84M7.5 14.25L5.106 5.272M6 20.25a.75.75 0 11-1.5 0 .75.75 0 011.5 0zm12.75 0a.75.75 0 11-1.5 0 .75.75 0 011.5 0z" />
                        </svg>
                        <CartBubble />
                    </Link>


                </div>

            </div>

        </header>

        <main> 
            <Suspense>{children}</Suspense>
        </main> 


    </>
); }  

    const SHOP_QUERY = gql` 
    query ShopInfo { 
        shop { 
            name 
            description 
        } 
        } 
    `;
I have try to make a const in Layout.server.jsx, also to create a Layout.client.jsx component but that doesn't work either.

我试着在Layout. server. jsx中创建一个常量,也试着创建一个Layout. client. jsx组件,但是也不起作用。

xuo3flqw

xuo3flqw1#

我对shopify-hydrogen不熟悉,但是我可以告诉您如何在tailwindcss中实现这一点
您必须使用relativeabsolutez-index tailwind类来覆盖页面内容上的导航栏。

逻辑:

使父relativez-index值小于将用于导航栏的子absolute div。

大型器械中的输出:

较小器械的输出:

代码:

<div class="md:bg-yellow-400 h-screen relative z-0 flex bg-gray-500">
      <div class="invisible md:visible bg-blue-400 w-1/3">
        <div class="flex h-full items-center justify-center text-4xl">
          Desktop Navbar
        </div>
      </div>
      <div class="text-4xl">
        The main content of the file and it has it's content all over the page
        and i want to build a navbar on top of this
      </div>
      <div
        class="absolute inset-y-0 left-0 z-10 bg-green-400 w-1/3 md:hidden"
      >
        <div class="flex h-full items-center justify-center text-4xl">
          Mobile Navbar
        </div>
      </div>
    </div>

您还可以使用此tailwind play链接

额外:使用汉堡菜单切换移动导航栏

大型设备上的输出

hamburger menu的小型设备中输出

点击hamburger menu

代码:

<body>
    <div class="bg-yellow-400 h-screen relative z-0 flex">
      <div class="hidden md:block bg-blue-400 w-1/3">
        <div class="flex h-full items-center justify-center text-4xl">
          Desktop Navbar
        </div>
      </div>
      <div class="text-4xl pl-24 md:p-0 main_content">
        The main content of the file and it has it's content all over the page
        and i want to build a navbar on top of this
      </div>
      <div
        class="mobile_navbar absolute inset-y-0 left-0 z-10 bg-green-400 w-1/3 hidden md:hidden"
      >
        <div class="flex h-full items-center justify-center text-4xl">
          Mobile Navbar
        </div>
      </div>
      <div
        class="md:hidden space-y-2 absolute hamburger_menu inset-y-0 left-0 p-4"
      >
        <span class="block w-8 h-1 bg-white"></span>
        <span class="block w-8 h-1 bg-white"></span>
        <span class="block w-8 h-1 bg-white"></span>
      </div>
    </div>
    <script type="text/javascript">
      document
        .querySelector(".hamburger_menu")
        .addEventListener("click", () => {
          console.log("Hello");
          document.querySelector(".mobile_navbar").classList.toggle("hidden");
        });

      document.querySelector(".main_content").addEventListener("click", () => {
        console.log("Touch me");
        console.log(
          document
            .querySelector(".mobile_navbar")
            .classList.contains("hidden") == false &&
            document.querySelector(".mobile_navbar").classList.toggle("hidden")
        );
      });
    </script>
  </body>

相关问题