gpt4 book ai didi

The number inside an object doesn't update in useState hook(对象中的编号不会在useState挂钩中更新)

转载 作者:bug小助手 更新时间:2023-10-26 21:10:33 25 4
gpt4 key购买 nike



const handleContentData = () => {
setHasMore(false);
dispatch(contentList(paramsData));
setParamsData((prevState) => ({
...prevState,
offset: prevState.offset + 24,
}));
setHasMore(true);
};


I have this code block and I want to add 24 to offset property each time this function invokes

我有这个代码块,并且我想在每次调用这个函数时添加24个偏移量属性


the initial offset is 0, after that 24 is shown but in other calls it just repeats 24 value

初始偏移量是0,在显示24之后,但在其他调用中,它只是重复24值


更多回答

Please provide a minimal reproducible example.

请提供一个最小的可重复使用的例子。

I use this function in a custom infinite scroll HOC, every time user scrolls down I call handleContentData function to get data from server but as I said after 24 the amount of offset doesn't change in other requests. the property value in prevState is updated but the amount of new state doesn't get update.

我在一个自定义的无限滚动中使用了这个函数,每次用户向下滚动时,我都会调用handleContent Data函数从服务器获取数据,但正如我在24小时后所说的,偏移量在其他请求中不会改变。PremState中的属性值已更新,但新状态的数量未更新。

Cannot reproduce with the given code: codesandbox.io/s/peaceful-babbage-frzr7w?file=/src/App.tsx

无法使用给定代码进行复制:codesandbox.io/s/peaceful-babbage-frzr7w?file=/src/App.tsx

优秀答案推荐

Write your component as follows

编写组件如下


// defining the state
const [obj, setObj] = useState({ title: "Hello", offset: 12 });

// Handle update function
const handleE = () => {
setObj((prevState) => ({
...prevState,
offset: prevState.offset + 24,
}));
};

return (
<div>
<button onClick={handleE}>Click</button>

<div>{obj.title}</div>
<div>{obj.offset}</div>
</div>
);

更多回答

The issue is I use this function in a custom infinite scroll HOC, every time user scrolls down I call handleContentData function to get data from server but as I said after 24 the amount of offset doesn't change in other requests. the property value in prevState is updated but the amount of state (like obj in your code) doesn't get update

问题是我在一个自定义的无限滚动中使用了这个函数,每次用户向下滚动时,我调用handleContent Data函数从服务器获取数据,但正如我在24之后所说的那样,偏移量在其他请求中不会改变。PremState中的属性值已更新,但状态的数量(如代码中的obj)不会更新

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