{"id":52072,"date":"2024-12-03T14:48:17","date_gmt":"2024-12-03T06:48:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52072\/"},"modified":"2024-12-03T14:48:17","modified_gmt":"2024-12-03T06:48:17","slug":"%e4%bd%bf%e7%94%a8-react-%e5%92%8c-tailwind-css-%e5%88%9b%e5%bb%ba%e5%8a%a8%e6%80%81%e4%ba%a7%e5%93%81%e5%b1%95%e7%a4%ba%e8%bd%ae%e6%92%ad-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52072\/","title":{"rendered":"\u4f7f\u7528 React \u548c Tailwind CSS \u521b\u5efa\u52a8\u6001\u4ea7\u54c1\u5c55\u793a\u8f6e\u64ad"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f7f\u7528 React \u548c Tailwind CSS \u521b\u5efa\u52a8\u6001\u4ea7\u54c1\u5c55\u793a\u8f6e\u64ad<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u7c73\u4e91\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u4f7f\u7528 React \u548c Tailwind CSS \u521b\u5efa\u52a8\u6001\u4ea7\u54c1\u5c55\u793a\u8f6e\u64ad\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241118\/1731939509673b4cb585da9.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528 React \u548c Tailwind CSS \u521b\u5efa\u52a8\u6001\u4ea7\u54c1\u5c55\u793a\u8f6e\u64ad\u63d2\u56fe\" alt=\"\u4f7f\u7528 React \u548c Tailwind CSS \u521b\u5efa\u52a8\u6001\u4ea7\u54c1\u5c55\u793a\u8f6e\u64ad\u63d2\u56fe\" \/><\/p>\n<p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u9010\u6b65\u4ecb\u7ecd\u4f7f\u7528 react \u548c tailwind css \u6784\u5efa\u6f02\u4eae\u7684\u54cd\u5e94\u5f0f\u4ea7\u54c1\u5c55\u793a\u8f6e\u64ad\u7684\u8fc7\u7a0b\u3002\u8be5\u8f6e\u64ad\u5c06\u5177\u6709\u5e73\u6ed1\u8fc7\u6e21\u3001\u81ea\u52a8\u548c\u624b\u52a8\u5bfc\u822a\u4ee5\u53ca\u9002\u5e94\u5404\u79cd\u5c4f\u5e55\u5c3a\u5bf8\u7684\u65f6\u5c1a\u8bbe\u8ba1\u3002<\/p>\n<h2> \u7b2c 1 \u6b65\uff1a\u8bbe\u7f6e\u9879\u76ee <\/h2>\n<p>\u9996\u5148\uff0c\u786e\u4fdd\u60a8\u6709\u4e00\u4e2a\u4f7f\u7528 tailwind css \u8bbe\u7f6e\u7684 react \u9879\u76ee\u3002\u5982\u679c\u60a8\u662f\u767d\u624b\u8d77\u5bb6\uff0c\u53ef\u4ee5\u4f7f\u7528 vite \u8fd9\u6837\u7684\u5de5\u5177\u6765\u5feb\u901f\u542f\u52a8\u4e00\u4e2a\u65b0\u9879\u76ee\u3002<\/p>\n<h2> \u7b2c 2 \u6b65\uff1a\u521b\u5efa\u4ea7\u54c1\u6570\u636e <\/h2>\n<p>\u5728 src\/data \u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a products.ts \u7684\u6587\u4ef6\u6765\u5b58\u50a8\u4ea7\u54c1\u4fe1\u606f\uff1a<\/p>\n<pre>export const products = [\n  {\n    id: 1,\n    name: \"premium wireless headphones\",\n    description: \"immerse yourself in crystal-clear sound with our latest noise-cancelling technology.\",\n    image: \"https:\/\/images.unsplash.com\/photo-1505740420928-5e560c06d30e?ixlib=rb-4.0.3&amp;ixid=m3wxmja3fdb8mhxwag90by1wywdlfhx8fgvufdb8fhx8fa%3d%3d&amp;auto=format&amp;fit=crop&amp;w=1470&amp;q=80\"\n  },\n  \/\/ add more products...\n]\n<\/pre>\n<h2> \u7b2c 3 \u6b65\uff1a\u521b\u5efa productcarousel \u7ec4\u4ef6 <\/h2>\n<p>\u5728 src\/components \u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6 productcarousel.tsx\uff1a<\/p>\n<pre>import react from 'react'\nimport { chevronleft, chevronright } from 'lucide-react'\n\ninterface product {\n  id: number\n  name: string\n  description: string\n  image: string\n}\n\ninterface productcarouselprops {\n  products: product[]\n}\n\nconst productcarousel: react.fc&lt;productcarouselprops&gt; = ({ products }) =&gt; {\n  \/\/ implement carousel logic here\n  return (\n    &lt;div classname=\"relative\"&gt;\n      {\/* carousel content *\/}\n    &lt;\/div&gt;\n  )\n}\n\nexport default productcarousel\n<\/pre>\n<h2> \u7b2c 4 \u6b65\uff1a\u5b9e\u73b0\u8f6e\u64ad\u903b\u8f91 <\/h2>\n<p>\u5728productcarousel\u7ec4\u4ef6\u5185\u90e8\uff0c\u5b9e\u73b0\u8f6e\u64ad\u903b\u8f91\uff1a<\/p>\n<ol>\n<li>\u6dfb\u52a0\u72b6\u6001\u4ee5\u8ddf\u8e2a\u5f53\u524d\u5e7b\u706f\u7247\uff1a <\/li>\n<\/ol>\n<pre>   const [currentslide, setcurrentslide] = react.usestate(0)\n<\/pre>\n<ol>\n<li>\u521b\u5efa\u5bfc\u822a\u51fd\u6570\uff1a <\/li>\n<\/ol>\n<pre>   const nextslide = () =&gt; setcurrentslide((prev) =&gt; (prev + 1) % products.length)\n   const prevslide = () =&gt; setcurrentslide((prev) =&gt; (prev - 1 + products.length) % products.length)\n<\/pre>\n<ol>\n<li>\u8bbe\u7f6e\u81ea\u52a8\u6ed1\u52a8\uff1a <\/li>\n<\/ol>\n<pre>   react.useeffect(() =&gt; {\n     const timer = setinterval(nextslide, 5000)\n     return () =&gt; clearinterval(timer)\n   }, [])\n<\/pre>\n<h2> \u7b2c 5 \u6b65\uff1a\u521b\u5efa\u8f6e\u64ad ui <\/h2>\n<p>\u66f4\u65b0productcarousel\u7ec4\u4ef6\u7684\u8fd4\u56de\u8bed\u53e5\uff1a<\/p>\n<pre>return (\n  &lt;div classname=\"relative overflow-hidden\"&gt;\n    &lt;div\n      classname=\"flex transition-transform duration-500 ease-out\"\n      style={{ transform: `translatex(-${currentslide * 100}%)` }}\n    &gt;\n      {products.map((product) =&gt; (\n        &lt;div key={product.id} classname=\"w-full flex-shrink-0\"&gt;\n          &lt;div classname=\"relative h-[60vh] md:h-[70vh] lg:h-[80vh]\"&gt;\n            &lt;img\n              src={product.image}\n              alt={product.name}\n              classname=\"w-full h-full object-cover\"\n            \/&gt;\n            &lt;div classname=\"absolute inset-0 bg-black bg-opacity-40 flex items-center justify-center\"&gt;\n              &lt;div classname=\"text-center text-white p-4 md:p-8 max-w-2xl\"&gt;\n                &lt;h2 classname=\"text-3xl md:text-4xl font-bold mb-4\"&gt;{product.name}&lt;\/h2&gt;\n                &lt;p classname=\"text-lg md:text-xl mb-6\"&gt;{product.description}&lt;\/p&gt;\n                &lt;button classname=\"bg-white text-black font-bold py-2 px-6 rounded-full hover:bg-gray-200 transition duration-300\"&gt;\n                  shop now\n                &lt;\/button&gt;\n              &lt;\/div&gt;\n            &lt;\/div&gt;\n          &lt;\/div&gt;\n        &lt;\/div&gt;\n      ))}\n    &lt;\/div&gt;\n    {\/* navigation arrows *\/}\n    &lt;button\n      onclick={prevslide}\n      classname=\"absolute left-4 top-1\/2 transform -translate-y-1\/2 bg-white bg-opacity-50 rounded-full p-2 hover:bg-opacity-75 transition duration-300\"\n    &gt;\n      &lt;chevronleft classname=\"w-6 h-6 text-black\" \/&gt;\n    &lt;\/button&gt;\n    &lt;button\n      onclick={nextslide}\n      classname=\"absolute right-4 top-1\/2 transform -translate-y-1\/2 bg-white bg-opacity-50 rounded-full p-2 hover:bg-opacity-75 transition duration-300\"\n    &gt;\n      &lt;chevronright classname=\"w-6 h-6 text-black\" \/&gt;\n    &lt;\/button&gt;\n    {\/* dots navigation *\/}\n    &lt;div classname=\"absolute bottom-4 left-0 right-0\"&gt;\n      &lt;div classname=\"flex items-center justify-center gap-2\"&gt;\n        {products.map((_, i) =&gt; (\n          &lt;button\n            key={i}\n            classname={`\n              w-3 h-3 rounded-full transition-all duration-300\n              ${currentslide === i ? 'bg-white scale-110' : 'bg-white bg-opacity-50'}\n            `}\n            onclick={() =&gt; setcurrentslide(i)}\n          \/&gt;\n        ))}\n      &lt;\/div&gt;\n    &lt;\/div&gt;\n  &lt;\/div&gt;\n)\n<\/pre>\n<h2> \u7b2c 6 \u6b65\uff1a\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 productcarousel <\/h2>\n<p>\u66f4\u65b0\u60a8\u7684 app.tsx \u4ee5\u4f7f\u7528 productcarousel \u7ec4\u4ef6\uff1a<\/p>\n<pre>import React from 'react'\nimport ProductCarousel from '.\/components\/ProductCarousel'\nimport { products } from '.\/data\/products'\n\nfunction App() {\n  return (\n    &lt;div className=\"min-h-screen bg-gray-100\"&gt;\n      &lt;header className=\"bg-white shadow-md py-4\"&gt;\n        &lt;div className=\"container mx-auto px-4\"&gt;\n          &lt;h1 className=\"text-3xl font-bold text-gray-800\"&gt;Product Showcase&lt;\/h1&gt;\n        &lt;\/div&gt;\n      &lt;\/header&gt;\n      &lt;main className=\"container mx-auto px-4 py-8\"&gt;\n        &lt;ProductCarousel products={products} \/&gt;\n      &lt;\/main&gt;\n    &lt;\/div&gt;\n  )\n}\n\nexport default App\n<\/pre>\n<h2> \u7ed3\u8bba <\/h2>\n<p>\u60a8\u73b0\u5728\u62e5\u6709\u4e86\u4e00\u4e2a\u4f7f\u7528 react \u548c tailwind css \u6784\u5efa\u7684\u6f02\u4eae\u3001\u54cd\u5e94\u5f0f\u4ea7\u54c1\u5c55\u793a\u8f6e\u64ad\u3002\u8be5\u8f6e\u64ad\u5177\u6709\u5e73\u6ed1\u8fc7\u6e21\u3001\u81ea\u52a8\u548c\u624b\u52a8\u5bfc\u822a\u529f\u80fd\uff0c\u5e76\u80fd\u5f88\u597d\u5730\u9002\u5e94\u4e0d\u540c\u7684\u5c4f\u5e55\u5c3a\u5bf8\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5b9a\u5236\u8bbe\u8ba1\uff0c\u6dfb\u52a0\u66f4\u591a\u4ea4\u4e92\u529f\u80fd\uff0c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u8bf7\u8bb0\u4f4f\u4f18\u5316\u60a8\u7684\u56fe\u50cf\u5e76\u5728\u5404\u79cd\u8bbe\u5907\u4e0a\u6d4b\u8bd5\u8f6e\u64ad\uff0c\u4ee5\u786e\u4fdd\u5728\u6240\u6709\u5e73\u53f0\u4e0a\u83b7\u5f97\u6700\u4f73\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u5b89\u88c5\u4e00\u53f0\u5854\u540a\u591a\u5c11\u94b1\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 React \u548c Tailwind CSS \u521b\u5efa\u52a8\u6001\u4ea7\u54c1\u5c55\u793a\u8f6e\u64ad \u6536\u85cf \u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u7c73\u4e91\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u4f7f\u7528 React \u548c Tailwind CSS \u521b\u5efa\u52a8\u6001\u4ea7\u54c1\u5c55\u793a\u8f6e\u64ad\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01 \u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u9010\u6b65\u4ecb\u7ecd\u4f7f\u7528 react \u548c tailwind css \u6784\u5efa\u6f02\u4eae\u7684\u54cd\u5e94\u5f0f\u4ea7\u54c1\u5c55\u793a\u8f6e\u64ad\u7684\u8fc7\u7a0b\u3002\u8be5\u8f6e\u64ad\u5c06\u5177\u6709\u5e73\u6ed1\u8fc7\u6e21\u3001\u81ea\u52a8\u548c\u624b\u52a8\u5bfc\u822a\u4ee5\u53ca\u9002\u5e94\u5404\u79cd\u5c4f\u5e55\u5c3a\u5bf8\u7684\u65f6\u5c1a\u8bbe\u8ba1\u3002 \u7b2c 1 \u6b65\uff1a\u8bbe\u7f6e\u9879\u76ee \u9996\u5148\uff0c\u786e\u4fdd\u60a8\u6709\u4e00\u4e2a\u4f7f\u7528 tailwind css \u8bbe\u7f6e\u7684 react \u9879\u76ee\u3002\u5982\u679c\u60a8\u662f\u767d\u624b\u8d77\u5bb6\uff0c\u53ef\u4ee5\u4f7f\u7528 vite \u8fd9\u6837\u7684\u5de5\u5177\u6765\u5feb\u901f\u542f\u52a8\u4e00\u4e2a\u65b0\u9879\u76ee\u3002 \u7b2c 2 \u6b65\uff1a\u521b\u5efa\u4ea7\u54c1\u6570\u636e \u5728 src\/data \u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a products.ts \u7684\u6587\u4ef6\u6765\u5b58\u50a8\u4ea7\u54c1\u4fe1\u606f\uff1a export const products = [ { id: 1, name: &#8220;premium wireless headphones&#8221;, description: &#8220;immerse yourself in crystal-clear [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-52072","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=52072"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52072\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}