欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

React 條件判斷實例詳解

 更新時間:2024年08月12日 10:42:52   作者:國產(chǎn)bug零零柒  
在 React 中,可以通過 JavaScript 的條件語句來動態(tài)渲染組件或元素,下面給大家分享幾種常用的在 React 中處理條件渲染的方法,感興趣的朋友跟隨小編一起看看吧

在 React 中,可以通過 JavaScript 的條件語句來動態(tài)渲染組件或元素。

以下是幾種常用的在 React 中處理條件渲染的方法:

1. 使用 if 語句

在 render 方法或函數(shù)組件的返回值中使用 if 語句來決定渲染內容。

實例

import React from 'react';
import ReactDOM from 'react-dom/client';
class MyComponent extends React.Component {
  render() {
    const isLoggedIn = this.props.isLoggedIn;
    let content;
    if (isLoggedIn) {
      content = <h1>Welcome back!</h1>;
    } else {
      content = <h1>Please sign up.</h1>;
    }
    return (
      <div>
        {content}
      </div>
    );
  }
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<MyComponent isLoggedIn={true} />);

2. 使用三元運算符

在 JSX 中,可以使用三元運算符進行簡潔的條件渲染。

實例

import React from 'react';
import ReactDOM from 'react-dom/client';
const MyComponent = (props) => {
  const isLoggedIn = props.isLoggedIn;
  return (
    <div>
      {isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign up.</h1>}
    </div>
  );
};
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<MyComponent isLoggedIn={true} />);

3. 使用邏輯與 (&&) 運算符

在 JSX 中,可以使用邏輯與運算符來進行條件渲染。如果條件為 true,則渲染后面的元素。

實例

import React from 'react';
import ReactDOM from 'react-dom/client';
const MyComponent = (props) => {
  const isLoggedIn = props.isLoggedIn;
  return (
    <div>
      {isLoggedIn && <h1>Welcome back!</h1>}
      {!isLoggedIn && <h1>Please sign up.</h1>}
    </div>
  );
};
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<MyComponent isLoggedIn={true} />);

4. 使用 switch 語句

在需要處理多個條件時,可以在 render 方法中使用 switch 語句。

實例

import React from 'react';
import ReactDOM from 'react-dom/client';
class MyComponent extends React.Component {
  render() {
    const userRole = this.props.userRole;
    let content;
    switch (userRole) {
      case 'admin':
        content = <h1>Welcome, Admin!</h1>;
        break;
      case 'user':
        content = <h1>Welcome, User!</h1>;
        break;
      case 'guest':
        content = <h1>Welcome, Guest!</h1>;
        break;
      default:
        content = <h1>Who are you?</h1>;
    }
    return (
      <div>
        {content}
      </div>
    );
  }
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<MyComponent userRole="admin" />);

小結

  • if 語句:適合在 render 方法或函數(shù)組件的返回值中使用來決定渲染內容。
  • 三元運算符:適合在 JSX 中進行簡潔的條件渲染。
  • 邏輯與 (&&) 運算符:適合在 JSX 中條件渲染,當條件為 true 時渲染元素。
  • switch 語句:適合處理多個條件,進行不同內容的渲染。

到此這篇關于React 條件判斷的文章就介紹到這了,更多相關React 條件判斷內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • React項目中使用Redux的?react-redux

    React項目中使用Redux的?react-redux

    這篇文章主要介紹了React項目中使用Redux的?react-redux,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • 詳解Redux的工作流程

    詳解Redux的工作流程

    這篇文章主要介紹了Redux的工作流程,redux是一個專門用于做狀態(tài)管理的JS庫,它可以在react、angular、vue等項目中,但基本與react配合使用,需要的朋友可以參考下
    2022-08-08
  • React合成事件及Test Utilities在Facebook內部進行測試

    React合成事件及Test Utilities在Facebook內部進行測試

    這篇文章主要介紹了React合成事件及Test Utilities在Facebook內部進行測試,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • React項目配置prettier和eslint的方法

    React項目配置prettier和eslint的方法

    這篇文章主要介紹了React項目配置prettier和eslint的相關知識,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • 解決React報錯Property value does not exist on type HTMLElement

    解決React報錯Property value does not exist&n

    這篇文章主要為大家介紹了React報錯Property value does not exist on type HTMLElement解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • React樣式?jīng)_突解決問題的方法

    React樣式?jīng)_突解決問題的方法

    本文主要介紹了React樣式?jīng)_突解決問題的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • React.js?Gird?布局編寫鍵盤組件

    React.js?Gird?布局編寫鍵盤組件

    這篇文章主要介紹了React.js?Gird?布局編寫鍵盤組件,Grid?布局則是將容器劃分成"行"和"列",產(chǎn)生單元格,然后指定"項目所在"的單元格,可以看作是二維布局
    2022-09-09
  • React實現(xiàn)虛擬滾動的三種思路詳解

    React實現(xiàn)虛擬滾動的三種思路詳解

    在??web??開發(fā)的過程中,或多或少都會遇到大列表渲染的場景,為了解決大列表造成的渲染壓力,便出現(xiàn)了虛擬滾動技術,本文主要介紹虛擬滾動的三種思路,希望對大家有所幫助
    2024-04-04
  • React-three-fiber使用初體驗

    React-three-fiber使用初體驗

    這篇文章主要為大家介紹了React-three-fiber的使用初體驗,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • React 源碼中的依賴注入方法

    React 源碼中的依賴注入方法

    這篇文章主要介紹了React 源碼中的依賴注入方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11

最新評論