如何在 ReactJS 中使用滑块组件?

原文:https://www . geeksforgeeks . org/how-用法-slider-in-component-reactjs/

滑块允许用户从一系列值中进行选择。React 的 Material UI 有这个组件可供我们使用,非常容易集成。我们可以在 ReactJS 中使用滑块组件,方法如下:

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

步骤 1: 使用以下命令创建一个反应应用程序:

npx create-react-app foldername

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

cd foldername

步骤 3: 创建 ReactJS 应用程序后,使用以下命令安装 material-ui 模块:

npm install @material-ui/core
npm install @material-ui/icons

项目结构:如下图。

项目结构

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

java 描述语言

import React from 'react';
import Typography from '@material-ui/core/Typography';
import Grid from '@material-ui/core/Grid';
import Slider from '@material-ui/core/Slider';
import VolumeUp from '@material-ui/icons/VolumeUp';
import VolumeDown from '@material-ui/icons/VolumeDown';

const App = () => {

  // Our States
  const [value, setValue] = React.useState(10);

  // Changing State when volume increases/decreases
  const changeVolume = (event, newValue) => {
    setValue(newValue);
  };

  return (
    <div style={{
      margin: 'auto',
      display: 'block',
      width: 'fit-content'
    }}>
      <h3>How to use Slider Component in ReactJS?</h3>
      <Typography id="continuous-slider" gutterBottom>
        Volume
      </Typography>
      <Grid container spacing={4}>
        <Grid item>
          <VolumeDown />
        </Grid>
        <Grid item xs>
          <Slider value={value} 
          onChange={changeVolume}
        />
        </Grid>
        <Grid item>
          <VolumeUp />
        </Grid>
      </Grid>
    </div>
  );
}

export default App;

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

npm start

输出:现在打开浏览器,转到http://localhost:3000/,会看到如下输出: