gpt4 book ai didi

How to use Tailwind UI (headless ui) components with Next JS server components?(如何将TailWind UI(Headless UI)组件与Next JS服务器组件配合使用?)

转载 作者:bug小助手 更新时间:2023-10-25 09:30:36 32 4
gpt4 key购买 nike



I'm trying to build a web app on Next JS with Tailwind UI provided React components. Tailwind UI is using headless ui behind the scenes.

我正在尝试建立一个下一个JS与尾风用户界面提供的反应组件的网络应用程序。顺风用户界面是在幕后使用无头用户界面。


By default, Next JS will build server side components, unless you add 'use client' at the top of a page. Unfortunately, this seems to be a requirement instead of an option in case of Tailwind UI provided code, otherwise it will throw an error like this on the picture.

默认情况下,Next JS将构建服务器端组件,除非您在页面顶部添加“使用客户端”。不幸的是,这似乎是必需的,而不是在TailWind UI提供代码的情况下的选项,否则它将在图片上抛出如下所示的错误。


enter image description here


So the question is, how to use Tailwind UI components (to move fast with the development without being a front-end genius), but still have the option to use server components with Next JS?

因此,问题是,如何使用TailWind UI组件(在不是前端天才的情况下快速推进开发),同时仍然可以选择将服务器组件与Next JS一起使用?


What I tried


Tried using Tailwind UI components without 'use client', but they throw an error. Only solution is to add 'use client' at the top of the .tsx (or .jsx) file.

已尝试在不使用客户端的情况下使用TailWind UI组件,但引发错误。唯一的解决方案是在.tsx(或.jsx)文件的顶部添加‘Use Client’。


更多回答
优秀答案推荐

You do everything right, this is the Next.js problem. I just wrote a broad explanation of this behaviour. Please see
https://stackoverflow.com/a/77077564/9047572

你做的每件事都是正确的,这就是Next.js的问题。我刚刚写了一篇关于这一行为的广泛解释。请参阅https://stackoverflow.com/a/77077564/9047572


TL:DR: you need that 'use client' directive to use client-only features. Once the component became client-side, its nested components are client-side too. No way to change or intercept this behavior.

TL:DR:您需要‘Use Client’指令才能使用仅限客户端的功能。一旦组件成为客户端,它的嵌套组件也是客户端。无法改变或拦截这种行为。


And TailwindCSS use some client-only features. Your error tells the same. It tells that headlessui.esm.js is a client-only component, so you can use this library in client-side components only.

Tailwincss使用一些仅限客户端的功能。你的错误也说明了这一点。它告诉我们,headlessui.esm.js是一个仅限客户端的组件,因此您只能在客户端组件中使用该库。


"Headless" doesn't mean it doesn't use some client-only bindings :)

“Headless”并不意味着它不使用某些仅限客户端的绑定:)


P.S. Short list of "client-only" features. Basically Nextjs will show you them in its errors, but anyway...

附注:“仅限客户端”功能的简短列表。基本上,Nextjs会在它的错误中向你展示它们,但无论如何...



  • useX hooks from React and Nextjs: useEffect, useState, useContext, useRouter, useNavigation...

  • quite everything from react-dom, cause there is no possibility of DOM manipulation on the server side

  • browser event listeners like onChange.


You better read the docs here

你最好读一下这里的文件


P.P.S. Looks like there is another directory 'use server', which I didn't know of 2 days ago. Worth checking.

另外,好像还有另外一个目录‘USE SERVER’,2天前我还不知道呢。值得一查。


更多回答

Thanks a lot! Can I ask you to edit your answer and a brief list of those "client-only" features? I'm a senior engineer but quite noob when it comes to front-end, especially React and NextJS. I would use your list to research the docs, and research the headlessui files themselves, so that I can use the components more consciously, especially when building out their hierarchy

非常感谢!我可以要求您编辑您的答案和那些“仅限客户端”功能的简短列表吗?我是一名高级工程师,但在前端,特别是Reaction和NextJS方面,我相当新手。我会使用您的列表来研究文档,并研究HeadlesSui文件本身,这样我就可以更有意识地使用这些组件,特别是在构建它们的层次结构时

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com