当前位置: 首页 > news >正文

【前端】 react项目使用bootstrap、useRef和useState之间的区别和应用

一、场景描述

我想写一个轮播图的程序,只是把bootstrap里面的轮播图拉过来就用上感觉不是很合适,然后我就想自己写自动轮播,因此,这篇文章里面只是自动轮播的部分,没有按键跟自动轮播的衔接部分。
Ps: 本文用的是函数式组件,因为新版本的react函数式组件用的比较多。

二、相关知识

2.1 怎么在react.js中使用bootstrap

首先,在react中引入bootstrap。

npm install bootstrap

然后在index.js中引入bootstrap,在后续组件中使用bootstrap的时候就不需要再引入。
需要同时引入css文件和js文件。

import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap/dist/js/bootstrap";

2.2 useEffect钩子

useEffect钩子是在渲染之后运行的程序。有一个作用是:根据React state控制非React组件。
在部分的内容中有三种写的方式:

  useEffect(() => {
    //代码
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);   //后面的依赖数据是空的,就是只在第一次渲染的时候运行

  useEffect(() => {
    //代码
    // eslint-disable-next-line react-hooks/exhaustive-deps
  });    //后面没有依赖,每次渲染的时候都会运行

  useEffect(() => {
    //代码
    // eslint-disable-next-line react-hooks/exhaustive-deps
  },[img]);    //后面的依赖是img就只在img改变的时候再运行

2.3useRef钩子

ref和state之间的区别在于state变化之后组件会重新渲染,ref变化之后组件不会重新渲染。ref在整个组件生命周期内保持不变。

import { useRef } from "react”;

let currentIndex1 = useRef(0); //定义ref
//html中代码
<button onClick={() => {
    console.log(currentIndex1.current);
    currentIndex1.current = currentIndex1.current + 1;
}}>+1</button>

三、轮播图的自动播放功能实现

这部分分为两个问题,一个是自动播放的index,自动播放就是定时器interval;另一个是通过class类定义当前显示的图片,然后设置动画。
这里定义index只能是state,因为ref变化不会渲染,这样根据index改变的class类可能就改变不了。
思路如下图所示。
在这里插入图片描述
整体代码如下:
Recommend.tsx

import { useRef } from "react";
import React, { useEffect, useState } from "react";
import hyRequest from "../../service/index";
const Recommend = function () {
  const [img, setImg] = useState<any[]>([]);
  let currentIndex1 = useRef(0);
  const [currentIndex, setCurrentIndex] = useState(0);
  useEffect(() => {
    hyRequest.get({ url: "/banner" }).then((res) => {
      console.log(res);
      setImg(res.banners);
      console.log(img);
    });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
 
  useEffect(() => {
    console.log("设置定时器");
    const interval = setInterval(() => {
      //console.log(img, img.length);
      //currentIndex.current = (currentIndex.current + 1) % img.length;
      //console.log(currentIndex.current);
      setCurrentIndex((preCurrentIndex) => (preCurrentIndex + 1) % img.length);
    }, 3000);
    return () => clearInterval(interval); // 清除定时器
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [img]);
  return (
    <div>
      <h2>推荐</h2>
      <div id="carouselExample" className="carousel slide">
        <div className="carousel-inner">
          {img.map((item, index) => {
            //console.log(currentIndex, item, index);
            return (
              <div
                key={index}
                className={`carousel-item ${
                  index === currentIndex ? "active" : ""
                } ${index === currentIndex - 1 ? "preActive" : ""} ${
                  index === currentIndex + 1 ? "nextActive" : ""
                }`}
              >
                <img src={item.imageUrl} className="d-block w-100" alt="..." />
              </div>
            );
          })}
        </div>
        <button
          className="carousel-control-prev"
          type="button"
          data-bs-target="#carouselExample"
          data-bs-slide="prev"
        >
          <span
            className="carousel-control-prev-icon"
            aria-hidden="true"
          ></span>
          <span className="visually-hidden">Previous</span>
        </button>
        <button
          className="carousel-control-next"
          type="button"
          data-bs-target="#carouselExample"
          data-bs-slide="next"
        >
          <span
            className="carousel-control-next-icon"
            aria-hidden="true"
          ></span>
          <span className="visually-hidden">Next</span>
        </button>
      </div>
      <div>{currentIndex1.current}</div>
      <button
        onClick={() => {
          console.log(currentIndex1.current);
          currentIndex1.current = currentIndex1.current + 1;
        }}
      >
        +1
      </button>
    </div>
  );
};
 
export default Recommend;

index.less

.nextActive {
  display: block !important;
  transform: translateX(100%);
  opacity: 0;
}
.active {
  transform: none;
  opacity: 1;
}
.preActive {
  display: block !important;
  transform: translateX(-100%);
  opacity: 0;
}
http://www.dtcms.com/a/20212.html

相关文章:

  • AWS上基于高德地图API验证Amazon Redshift里国内地址数据正确性的设计方案
  • AI法理学与责任归属:技术演进下的法律重构与伦理挑战
  • 【问】强学如何支持 迁移学习呢?
  • 网络安全威胁是什么
  • 【STM32】江科大STM32学习笔记汇总(已完结)
  • Ubuntu 系统迁移
  • C语言(枚举类型)
  • C++ ——this指针
  • Rhel Centos环境开关机自动脚本
  • flutter本地推送 flutter_local_notifications的使用记录
  • Java面试题总结 - Java集合篇(附答案)
  • 一种访问网络中主机图片的方法
  • 深度学习框架PyTorch
  • 4090单卡挑战DeepSeek r1 671b:尝试量化后的心得的分享
  • 鸿蒙Next开发-添加水印以及点击穿透设置
  • c++中什么时候应该使用final关键字?
  • 143,【3】 buuctf web [GYCTF2020]EasyThinking
  • 【ISO 14229-1:2023 UDS诊断(会话控制0x10服务)测试用例CAPL代码全解析③】
  • 强化学习-NPG
  • Zbrush导入笔刷
  • 解锁电商数据宝藏:淘宝商品详情API实战指南
  • 内容中台构建高效数字化内容管理新范式
  • 【ISO 14229-1:2023 UDS诊断全量测试用例清单系列:第十三节】
  • 硬件开发笔记(三十四):AHD转MIPI国产方案详解XS9922B(一):芯片方案介绍
  • kubekey一键部署k8s高可用与kubesphere
  • 图像质量评价指标-UCIQE-UIQM
  • Ext系列文件系统
  • CAS单点登录(第7版)14.服务与应用
  • HCIA项目实践--静态路由的总结和简单配置
  • CAS单点登录(第7版)23.Webflow 管理