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值
更多回答
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中的属性值已更新,但新状态的数量未更新。
优秀答案推荐
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)不会更新
我是一名优秀的程序员,十分优秀!