反应按钮生命周期

发布于 2025-01-10 16:34:15 字数 2884 浏览 0 评论 0原文

这是我的 hacknews 项目,我不知道为什么这个按钮不起作用,

// popular
let curPos = 0;
let position = 0;
const BOX_WIDTH = 220;
const items = document.querySelector(".home_popular");
function prev() {
  if (curPos > 0) {
    position += BOX_WIDTH;
    items.style.transform = `translateX(${position}px)`;
    curPos = curPos - 1;
  }
}

function next() {
  if (curPos < 4) {
    position -= BOX_WIDTH;
    items.style.transform = `translateX(${position}px)`;
    curPos = curPos + 1;
  }
}
//new
let new_curPos = 0;
let new_position = 0;
const NEW_BOX_WIDTH = 282;

const new_items = document.querySelector(".box_container");
function new_prev() {
  if (new_curPos > 0) {
    new_position += NEW_BOX_WIDTH;
    new_items.style.transform = `translateX(${new_position}px)`;
    new_curPos = new_curPos - 1;
  }
}
function new_next() {
  if (new_curPos < 3) {
    new_position -= NEW_BOX_WIDTH;
    new_items.style.transform = `translateX(${new_position}px)`;
    new_curPos = new_curPos + 1;
  }
}

function Home() {
  return (
    <div className="container">
      <div className="main_word flex">
        <div className="word_home">
          <h2>Check out today's</h2>
          <h2>popular information</h2>
        </div>

        <img src={`/assets/headline.svg`} alt="star" className="star" />
      </div>

      <div className="home_popular">
        <HomeContents />
      </div>

      <button className="move_btn_right" onClick={next}>
        <img src={`/assets/arrow_forward.svg`} alt="" />
      </button>
      <button className="move_btn_left" onClick={prev}>
        <img src={`/assets/arrow_back.svg`} alt="" />
      </button>

      <div className="today_new">
        <p className="today_new_word">Today's New!</p>

        <div className="box_container">
          <div className="box4">
            <HomeContentsNew />
          </div>
          <div className="box4">
            <HomeContentsAsk />
          </div>
          <div className="box4">
            <HomeContentsShow />
          </div>
          <div className="box4">
            <HomeContentsJobs />
          </div>
        </div>

        <button className="today_new_btn_right" onClick={new_next}>
          <img src={`/assets/arrow_forward.svg`} alt="" />
        </button>
        <button className="today_new_btn_left" onClick={new_prev}>
          <img src={`/assets/arrow_back.svg`} alt="" />
        </button>
      </div>
    </div>
  );
}

export default Home;

这是我的主页代码,这个项目使用 React!这段代码的生命周期有问题吗?

此代码错误是

** 类型错误 无法读取 null 的属性(读取“样式”) **

我想知道如何在反应中使触摸滑动

我想将此按钮更改为滑块 帮我

this is my hacknews project i don't know why this button not working

// popular
let curPos = 0;
let position = 0;
const BOX_WIDTH = 220;
const items = document.querySelector(".home_popular");
function prev() {
  if (curPos > 0) {
    position += BOX_WIDTH;
    items.style.transform = `translateX(${position}px)`;
    curPos = curPos - 1;
  }
}

function next() {
  if (curPos < 4) {
    position -= BOX_WIDTH;
    items.style.transform = `translateX(${position}px)`;
    curPos = curPos + 1;
  }
}
//new
let new_curPos = 0;
let new_position = 0;
const NEW_BOX_WIDTH = 282;

const new_items = document.querySelector(".box_container");
function new_prev() {
  if (new_curPos > 0) {
    new_position += NEW_BOX_WIDTH;
    new_items.style.transform = `translateX(${new_position}px)`;
    new_curPos = new_curPos - 1;
  }
}
function new_next() {
  if (new_curPos < 3) {
    new_position -= NEW_BOX_WIDTH;
    new_items.style.transform = `translateX(${new_position}px)`;
    new_curPos = new_curPos + 1;
  }
}

function Home() {
  return (
    <div className="container">
      <div className="main_word flex">
        <div className="word_home">
          <h2>Check out today's</h2>
          <h2>popular information</h2>
        </div>

        <img src={`/assets/headline.svg`} alt="star" className="star" />
      </div>

      <div className="home_popular">
        <HomeContents />
      </div>

      <button className="move_btn_right" onClick={next}>
        <img src={`/assets/arrow_forward.svg`} alt="" />
      </button>
      <button className="move_btn_left" onClick={prev}>
        <img src={`/assets/arrow_back.svg`} alt="" />
      </button>

      <div className="today_new">
        <p className="today_new_word">Today's New!</p>

        <div className="box_container">
          <div className="box4">
            <HomeContentsNew />
          </div>
          <div className="box4">
            <HomeContentsAsk />
          </div>
          <div className="box4">
            <HomeContentsShow />
          </div>
          <div className="box4">
            <HomeContentsJobs />
          </div>
        </div>

        <button className="today_new_btn_right" onClick={new_next}>
          <img src={`/assets/arrow_forward.svg`} alt="" />
        </button>
        <button className="today_new_btn_left" onClick={new_prev}>
          <img src={`/assets/arrow_back.svg`} alt="" />
        </button>
      </div>
    </div>
  );
}

export default Home;

this is my main page code this project use React! Is there a problem with this code's life cycle?

this code error is

**
TypeError
Cannot read properties of null (reading 'style')
**

and i want to know how to make touch slide in react

i want to change this button to slider
help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绻影浮沉 2025-01-17 16:34:15

问题是,当您尝试使用 items 访问它们的 style 属性时,itemsnew_itemsnull .style.transformnew_items.style.transform

尝试在可选链接之前添加条件, 例如。

像这样:

items?.style?.transform

The problem is that items and new_items are null when you try to access their style property with items.style.transform or new_items.style.transform.

Try adding a condition before or optional chaining, for example.

Like this:

items?.style?.transform
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文