📜  React Suite 切换组件

📅  最后修改于: 2022-05-13 01:56:45.988000             🧑  作者: Mango

React Suite 切换组件

React Suite 是一个流行的前端库,包含一组专为中间平台和后端产品设计的 React 组件。切换 组件允许用户在两个值之间进行选择。 我们可以在 ReactJS 中使用以下方法来使用 React Suite Toggle 组件。

切换道具:

checkedIt is used for the checked state (Controlled).
checkedChildren:It is used to check the display content.
classPrefix It is used to denote the prefix of the component CSS class.
defaultChecked It is used to denote the default checked.
disabled: It is used to disable the component.
onChangeIt is a callback function fired when the state changes.
sizeIt is used to denote the toggle size.
unCheckedChildren:It is used to unselected the display content.

创建 React 应用程序并安装模块:

  • 第 1 步:使用以下命令创建一个 React 应用程序:

    npx create-react-app foldername

  • 第 2 步:创建项目文件夹(即文件夹名称)后使用以下命令移动到该文件夹:

    cd foldername
  • 第 3 步:创建 ReactJS 应用程序后,安装 必需的 模块使用以下命令:

    npm install rsuite

项目结构:它将如下所示。

项目结构

示例:现在在App.js文件中写下以下代码。在这里,App 是我们编写代码的默认组件。

App.js
// Importing required libraries
import React from 'react'
import 'rsuite/dist/styles/rsuite-default.css';
import { Toggle } from 'rsuite';
  
export default function App() {
  
  // State for the current toggle value
  const [currentValue, setCurrentValue] = React.useState(0)
  
  return (
    
      

React Suite Toggle Component

       { setCurrentValue(value) }}       />       {currentValue === true ? "Toggle On" : "Toggle Off"}     
  ); }


运行应用程序的步骤:从项目的根目录使用以下命令运行应用程序:

npm start

输出:

现在打开浏览器并转到http://localhost:3000/ ,您将看到以下输出:

参考: https://rsuitejs.com/components/toggle/