」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > React 中的樣式

React 中的樣式

發佈於2024-08-01
瀏覽:995

Styling in React

介紹

樣式是 Web 開發的重要方面,可確保您的應用程式具有視覺吸引力且使用者友好。 React 提供了多種設定元件樣式的方法,從傳統的 CSS 和 Sass 到現代的 CSS-in-JS 解決方案(例如 Styled-Components)。本週,我們將深入研究這些方法,並學習如何在您的 React 專案中有效地應用它們。

React 中樣式的重要性

正確的樣式可以增強使用者體驗,提高可用性,並使您的應用程式更具吸引力。了解不同的樣式技術可以讓您選擇最適合您的特定專案需求的方法。

傳統CSS

在 React 中使用 CSS:

  • 基本範例
  import React from 'react';
  import './App.css';

  function App() {
      return (
          

Hello, World!

); } export default App;
  • App.css
  .container {
      text-align: center;
      padding: 20px;
  }

  .title {
      color: blue;
      font-size: 2em;
  }

CSS 模組:

  • 例子
  import React from 'react';
  import styles from './App.module.css';

  function App() {
      return (
          

Hello, World!

); } export default App;
  • App.module.css
  .container {
      text-align: center;
      padding: 20px;
  }

  .title {
      color: blue;
      font-size: 2em;
  }

使用 Sass

安裝 Sass:

  • 安裝指令
  npm install node-sass

在 React 使用 Sass:

  • App.scss
  $primary-color: blue;
  $padding: 20px;

  .container {
      text-align: center;
      padding: $padding;
  }

  .title {
      color: $primary-color;
      font-size: 2em;
  }
  • 應用程式元件
  import React from 'react';
  import './App.scss';

  function App() {
      return (
          

Hello, World!

); } export default App;

Sass 嵌套樣式:

  • 例子
  .container {
      text-align: center;
      padding: 20px;

      .title {
          color: blue;
          font-size: 2em;

          &:hover {
              color: darkblue;
          }
      }
  }

樣式元件

樣式組件簡介:

  • 定義:使用標記範本文字來設計 React 元件樣式的函式庫。
  • 安裝
  npm install styled-components

使用樣式元件:

  • 例子
  import React from 'react';
  import styled from 'styled-components';

  const Container = styled.div`
      text-align: center;
      padding: 20px;
  `;

  const Title = styled.h1`
      color: blue;
      font-size: 2em;

      &:hover {
          color: darkblue;
      }
  `;

  function App() {
      return (
          
              Hello, World!
          
      );
  }

  export default App;

使用樣式元件進行主題化:

  • 創建主題
  import { ThemeProvider } from 'styled-components';

  const theme = {
      colors: {
          primary: 'blue',
          secondary: 'darkblue'
      },
      spacing: {
          padding: '20px'
      }
  };

  function App() {
      return (
          
              
                  Hello, World!
              
          
      );
  }
  • 使用主題值
  import styled from 'styled-components';

  const Container = styled.div`
      text-align: center;
      padding: ${(props) => props.theme.spacing.padding};
  `;

  const Title = styled.h1`
      color: ${(props) => props.theme.colors.primary};
      font-size: 2em;

      &:hover {
          color: ${(props) => props.theme.colors.secondary};
      }
  `;

結論

在 React 中選擇正確的樣式方法取決於您的專案要求和個人喜好。傳統的 CSS 和 Sass 提供熟悉性和簡單性,而 Styled-Components 提供動態和範圍內的樣式功能。掌握這些技術將幫助您建立美觀且可維護的使用者介面。

進一步學習的資源

  • 線上課程:Udemy、Pluralsight 和 freeCodeCamp 等網站提供有關 React 樣式技術的課程。
  • :Adam Boduch 的“React 和 React Native”和 Azat Mardan 的“React Quickly”。
  • 文件與參考文獻
    • 樣式元件文件
    • Sass 文件
    • React CSS 模組文件
  • 社群:加入 Stack Overflow、Reddit 和 GitHub 等平台上的開發者社群以獲得支援和交流。
版本聲明 本文轉載於:https://dev.to/suhaspalani/styling-in-react-1534?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3