gpt4 book ai didi

reactjs - Uncaught Error : Cannot add node 1 because a node with that id is already in the Store

转载 作者:行者123 更新时间:2023-12-05 03:38:07 26 4
gpt4 key购买 nike

在尝试使用该函数时,我收到一个错误,如 Uncaught Error: Cannot add node 1 because a node with that id is already in the Store.

这是我的功能:

import { useEffect, useState } from "react";
import "./styles.css";

export default function App() {
const [value, setValue] = useState(0);
const slides = ["one", "two", "thre"];
let size = slides.length;

useEffect(() => {
setValue(size);
}, [size]);

const prev = () => {
if (size && size < 1 && size && size > slides.length) return;
size--;
};

const next = () => {
if (size && size < 1 && size && size > slides.length) return;
size++;
};

return (
<div className="slider">
<a href="/" className="prev" onClick={prev}>
Previous
</a>
<h2>Barking Road, London {value}</h2>
<a href="/" className="next" onClick={next}>
Next
</a>
</div>
);
}

我想允许用户点击直到最小 1 和最大 3。但它不起作用。

LiveDemo

最佳答案

你实际上并不需要 useEffect为此,还要更改您的 <a>标记为 <button>

工作片段:

    function App() {
const [value, setValue] = React.useState(1);
const slides = ["one", "two", "thre"];

const prev = () => {
if (value > 1) setValue(value - 1);
};

const next = () => {
if (value < slides.length) setValue(value + 1);
};

return (
<div className="slider">
<button className="prev" onClick={prev}>
Previous
</button>
<h2>Barking Road, London {value}</h2>
<button className="next" onClick={next}>
Next
</button>
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="root"></div>

关于reactjs - Uncaught Error : Cannot add node 1 because a node with that id is already in the Store,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69070889/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com