如何重新验证与SWR关闭反应模式的数据?
我正在尝试重新验证
reaction-modal 在NextJS项目中使用SWR关闭。
我正在使用swr
挂钩。
const { data, error, isValidating } = useSWR(
process.env.NEXT_PUBLIC_APP_URL + `/api/users`,
fetcher,{
revalidateIfStale: true,
revalidateOnFocus: true,
revalidateOnMount:true,
}
);
useEffect(() => {
if (data) {
setUsers(data.users);
}
}, [data, isValidating, users]);
//handle loading..
//handle error..
return (
<main className="mx-auto max-w-7xl ">
<Header title="All users"/>
<UsersList users={users} />
</main>
);
我正在获取用户列表并显示它们。
const usersList = users.((user) => (
<div className="space-x-5 text-sm" key={user.id}>
{user.name}
<DisableModal id={user.id} isDisabled={user.active}/>
</div>
));
我有一个React模式,一旦我用句柄单击禁用了用户,就可以使用户禁用用户。
当模态关闭时,数据没有被淘汰。 这是来自文档的样本模式。
当我关闭模式时,可以看到用户列表。它们没有刷新,也没有使用SWR使用重新验证。
export const DisableModal = ({
id,
isDisabled,
}) => {
const [disableModalIsOpen, setDisableModalIsOpen] = useState(false);
function closeDisableModal() {
setDisableModalIsOpen(false);
}
function openPublishModal() {
setDisableModalIsOpen(true);
}
const handleDisableUser = async () => {
//disable logic in rest call.
closeDisableModal();
}
....
}
I am trying to revalidate
the data on react-modal close using SWR in a NextJS project.
I am using the SWR
hook like so.
const { data, error, isValidating } = useSWR(
process.env.NEXT_PUBLIC_APP_URL + `/api/users`,
fetcher,{
revalidateIfStale: true,
revalidateOnFocus: true,
revalidateOnMount:true,
}
);
useEffect(() => {
if (data) {
setUsers(data.users);
}
}, [data, isValidating, users]);
//handle loading..
//handle error..
return (
<main className="mx-auto max-w-7xl ">
<Header title="All users"/>
<UsersList users={users} />
</main>
);
I am fetching a list of users and displaying them.
const usersList = users.((user) => (
<div className="space-x-5 text-sm" key={user.id}>
{user.name}
<DisableModal id={user.id} isDisabled={user.active}/>
</div>
));
I have a react modal that allows us to disable the users, once I have disabled the users with handle click.
When the modal closes the data is not being refetched.
This is a sample modal from the docs.
When I close the modal, and can see the list of users. They are not refreshed and not using revalidations with use SWR.
export const DisableModal = ({
id,
isDisabled,
}) => {
const [disableModalIsOpen, setDisableModalIsOpen] = useState(false);
function closeDisableModal() {
setDisableModalIsOpen(false);
}
function openPublishModal() {
setDisableModalIsOpen(true);
}
const handleDisableUser = async () => {
//disable logic in rest call.
closeDisableModal();
}
....
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
突变
代码>在模态中触发模式中的回调。调用
突变(process.env.next_public_app_url +'/api/用户')
将使用给定的键向SWR挂钩向SWR挂载。意思是usesingwr(process.env.next_public_app_url +'/api/users',fetcher,{...})
挂钩将重新运行并返回更新的用户数据。You can revalidate the data manually using
mutate
when theonAfterClose
callback in the modal gets triggered.Calling
mutate(process.env.NEXT_PUBLIC_APP_URL + '/api/users')
will broadcast a revalidation message to SWR hook with that given key. Meaning theuseSWR(process.env.NEXT_PUBLIC_APP_URL + '/api/users', fetcher, { ... })
hook will re-run and return the updated users data.